I am trying to get a value difference (integer) between the years of 2 different dates in VBA and take also month (for example 2020 - 2019, the result I would want is 1) but my code only give me a date. Any ideas?
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
CodePudding user response:
You can use DateDiff:
DateDiff("yyyy", dat1, dat2)
With your code:
...
With PnLD1WS
.Cells(i, 164).Value = DateDiff("yyyy", .Cells(i, 13), PnLD1WS.Cells(i, 3))
End with
....