Hi Tuff,
Caclulating the number of years, months and days between two dates isn't as straightforward as it might seem (as you've found)!
The following macro, which you could convert to a user-defined function expresses the period between two dates in years, months and days. Most of the work goes into calculating and returning the elapsed whole years - the elapsed months and days are just extras.
Sub CalcTerm()
Dim StartDate As Date, EndDate As Date, Include As String
Dim Years As Integer, Months As Integer, Days As Integer, DaysInMonth As Integer
On Error GoTo ExitSub
StartDate = InputBox("Enter the Start Date in dd/mm/yyyy format")
EndDate = InputBox("Enter the End Date in dd/mm/yyyy format")
Include = UCase(Left(InputBox("Include both the Start and End Dates? (Y/N)"), 1))
If (Month(EndDate) = 2) Then
DaysInMonth = 28 + (Month(EndDate) = 2) * ((Year(EndDate) Mod 4 = 0) + (Year(EndDate) Mod 400 = 0) - (Year(EndDate) Mod 100 = 0))
Else
DaysInMonth = 31 - (Month(EndDate) = 4) - (Month(EndDate) = 6) - (Month(EndDate) = 9) - (Month(EndDate) = 11)
End If
Years = Year(EndDate) - Year(StartDate) + (Month(EndDate) < Month(StartDate)) + (Month(EndDate) = Month(StartDate)) * (Day(EndDate) < Day(StartDate) + (Include = "Y"))
Months = (12 + Month(EndDate) - Month(StartDate) + (Day(EndDate) < Day(StartDate) + (Include = "Y"))) Mod 12
Days = (DaysInMonth + Day(EndDate) - Day(StartDate) - (Include = "Y")) Mod DaysInMonth
MsgBox "The term is " & Years & " year(s) " & Months & " month(s) " & Days & " day(s)."
ExitSub:
End Sub
If you wanted to use a formula for calculating just the elapsed years, you'd need to mimic this bit of code:
Years = Year(EndDate) - Year(StartDate) + (Month(EndDate) < Month(StartDate)) + (Month(EndDate) = Month(StartDate)) * (Day(EndDate) < Day(StartDate) + (Include = "Y"))
where (Include = "Y") is replaced by 0 to count only the end date or 1 to count both the start and end dates.
Cheers