macropod was telling us:
macropod nous racontait que :
As I've said before, vba's datediff doesn't work for calculating a
person's age - at least not without a lot of extra effort. When
datediff calculates the elapsed years, it just does a year number
subtraction without regard to months and days. So:
Sub Test()
MsgBox DateDiff("yyyy", "31 / 12 / 2003", "1 / 1 / 2004")
End Sub
returns '1', for a 1-day difference! Not real clever. At least
datedif as a worksheet function in Excel takes those extra issues
into account.
How about this, using DateDiff to get a rough estimate? I say "rough"
because the weakness lies in the number of days per moth.
'_______________________________________
Sub RoughDateDiffEstimate()
Dim DayDiff As Long
Dim NumYear As Long
Dim NumMonth As Long
Dim NumDay As Long
Dim CheckNum As Single
Dim Date1 As Date
Dim Date2 As Date
Date1 = "2 / 1 / 2004"
Date2 = "3 / 1 / 2004"
DayDiff = DateDiff("d", Date1, Date2)
CheckNum = DayDiff / 365
'More than one year if result > 1
If CheckNum > 1 Then
NumYear = DayDiff \ 365
'get remainder
DayDiff = DayDiff Mod 365
Else
NumYear = 0
End If
'we will averge out the months to 30 days...
CheckNum = DayDiff / 30
'More than one month if result > 1
If CheckNum > 1 Then
NumMonth = DayDiff \ 30
'get remainder
DayDiff = DayDiff Mod 30
NumDay = DayDiff
Else
NumMonth = 0
NumDay = DayDiff
End If
MsgBox "There are " & NumYear & " year(s), " & NumMonth & " month(s) and " _
& NumDay & " day(s) between " & Date1 & " and " & Date2 _
& ".", vbInformation, "Result"
End Sub
'_______________________________________
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org