从日期 VBA 获取整数值
Getting an integer value from dates VBA
我正在尝试获取 VBA 中 2 个不同日期的年份之间的值差(整数)并且还取月(例如 2020 - 2019,我想要的结果是 1)但是我的代码只给我一个日期。有什么想法吗?
For i = 2 To PnLD1WS.Cells(1, 1).End(xlDown).Row
PnLD1WS.Cells(i, 164).Value = ((((DateSerial(Year(PnLD1WS.Cells(i, 13)), 0, 0) - DateSerial(Year(PnLD1WS.Cells(i, 3)), 0, 0)) * 12) + (DateSerial(0, (Month(PnLD1WS.Cells(i, 13))), 0) - (DateSerial(0, (Month(PnLD1WS.Cells(i, 3))), 0)))))
Next i
您可以使用 DateDiff:
DateDiff("yyyy", dat1, dat2)
使用您的代码:
...
With PnLD1WS
.Cells(i, 164).Value = DateDiff("yyyy", .Cells(i, 13), PnLD1WS.Cells(i, 3))
End with
....
我正在尝试获取 VBA 中 2 个不同日期的年份之间的值差(整数)并且还取月(例如 2020 - 2019,我想要的结果是 1)但是我的代码只给我一个日期。有什么想法吗?
For i = 2 To PnLD1WS.Cells(1, 1).End(xlDown).Row
PnLD1WS.Cells(i, 164).Value = ((((DateSerial(Year(PnLD1WS.Cells(i, 13)), 0, 0) - DateSerial(Year(PnLD1WS.Cells(i, 3)), 0, 0)) * 12) + (DateSerial(0, (Month(PnLD1WS.Cells(i, 13))), 0) - (DateSerial(0, (Month(PnLD1WS.Cells(i, 3))), 0)))))
Next i
您可以使用 DateDiff:
DateDiff("yyyy", dat1, dat2)
使用您的代码:
...
With PnLD1WS
.Cells(i, 164).Value = DateDiff("yyyy", .Cells(i, 13), PnLD1WS.Cells(i, 3))
End with
....