如果是宏,我的错误是什么?返回的值采用时间格式

时间:2016-06-15 21:42:15

标签: excel vba date datediff

以下是Sub计算2个日期之间的年份。单元格(i,3)中没有显示任何值,当我更新到MsgBox时,会显示时间值。在此先感谢您的帮助。

Sub EE_DatedIF()
    Dim wb1 As Workbook
    Dim i As Long
    Dim LastRow1 As Long
    Dim yrDiff As Long
    Dim d1 As Date
    Dim d2 As Date

    Set wb1 = Workbooks("macro all client v.01.xlsm")

    LastRow1 = wb1.Sheets("Carrier").range("E:E").Find("", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    For i = 10 To LastRow1
        d1 = wb1.Sheets("Settings").Cells(i, 1)
        d2 = wb1.Sheets("Carrier").Cells(i, 24)
        yrDiff = DateDiff("yy", d1, d2)
        wb1.Sheets("Settings").Cells(i, 3) = yrDiff
    Next i
End Sub

1 个答案:

答案 0 :(得分:1)

在其他问题中,您尝试检索E列中最后一个填充的行不应视为可靠。更接近这一点的更好。

With wb1.Sheets("Carrier").Range("E:E")
    lastRow1 = .Find("*", after:=.Cells(1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With