Forum Discussion
RonG_Fresno
Aug 24, 2020Copper Contributor
datedif giving random returns
I am using the datedif(BegDate,EndDate,"M") function to build a depreciation schedule. The column headings contain the current date for the depreciation (1/31/20, 2/29/20, etc.). Datedif compares the...
- Aug 26, 2020
RonG_Fresno, you're welcome!
HansVogelaar
Aug 24, 2020MVP
As you have found, DATEDIF is not entirely dependable. In your example, Excel returns 49 for 02/29/2020 since 29 is less than 31.
You might use a custom VBA function:
Function DDif(StartDate As Date, EndDate As Date, Interval As String) As Long
DDif = DateDiff(Interval, StartDate, EndDate)
End Function
Use like this:
=DDif(B1, C1, "m")
with the start date in B1 and the end date in C1.
RonG_Fresno
Aug 24, 2020Copper Contributor
HansVogelaar Thanks for your response. You also clued me in that the problem is because of different days in each month. I tried using datedif() with a +3 (to keep all months with at least 31 days) after the end date and it seems to work right. I suspect that I may run into a problem when my +3 overlaps another date some time.