我在 dated if 宏中的错误是什么?返回的值是时间格式
What is my error in the dated if macro? The value returned is in a a time format
下面是计算 2 个日期之间的年份的 Sub。单元格 (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
除其他问题外,您尝试检索 E 列中最后填充的行不应被视为可靠。最好有更接近这个的东西。
With wb1.Sheets("Carrier").Range("E:E")
lastRow1 = .Find("*", after:=.Cells(1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With
下面是计算 2 个日期之间的年份的 Sub。单元格 (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
除其他问题外,您尝试检索 E 列中最后填充的行不应被视为可靠。最好有更接近这个的东西。
With wb1.Sheets("Carrier").Range("E:E")
lastRow1 = .Find("*", after:=.Cells(1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End With