Wow! talk about overkill on the VBA solution ...
Macro, I can understand your use of DIY date solutions in fields where
there's no alternative, but in VBA, this is just idiotic. Use DateDiff(),
for heaven's sake.
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.
Cheers
> Wow! talk about overkill on the VBA solution ...
>
[quoted text clipped - 50 lines]
> > Checked by AVG anti-virus system (http://www.grisoft.com).
> > Version: 6.0.788 / Virus Database: 533 - Release Date: 1/11/2004
Martin Seelhofer - 11 Nov 2004 18:32 GMT
Hey there
> 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.
Well, if that's the only problem, where's the "lot of extra effort", then?
Function YearDiff(ByVal d1 As Date, ByVal d2 As Date) As Long
Dim d As Date ' temp var
' force d1 to be <= d2
If d1 > d2 Then
d = d1
d1 = d2
d2 = d
End If
' calculate difference
YearDiff = DateDiff("yyyy", d1, d2)
' pad the result
If Month(d2) < Month(d1) Or _
(Month(d2) = Month(d1) And Day(d2) < Day(d1)) Then
YearDiff = YearDiff - 1
End If
End Function
Cheers,
Martin
macropod - 11 Nov 2004 20:31 GMT
And now for the months and days, with a formatted output as per my example?
> Hey there
>
[quoted text clipped - 29 lines]
> Cheers,
> Martin
macropod - 12 Nov 2004 22:05 GMT
BTW Martin,
On what date would a person born on 29/2/2004 celebrate their 1st birthday
under your algorithm?
Cheers
> Hey there
>
[quoted text clipped - 29 lines]
> Cheers,
> Martin
Martin Seelhofer - 14 Nov 2004 22:56 GMT
Hello again
While I fully agree with you that those simple solutions suggested
in this thread don't produce *perfect* results, you might want to
have another look at the original question:
> I am doing a userform which asks the user to enter date of birth from
> a list box and was wondering if anyone knows the code which will
> calculate the age of that person on their last birthday.
Seen that "on their last birthday" thing? Looks like the original poster
was interested in the number of years only...
By the way...
> On what date would a person born on 29/2/2004 celebrate their 1st birthday
> under your algorithm?
That would be 2005-03-01. What did *you* expect?
Cheers,
Martin
macropod - 15 Nov 2004 07:42 GMT
Convention has it that persons born on Feb 29th celebrate their birthday on
the 29th of February, be it the 28th or 29th.
> Hello again
>
[quoted text clipped - 18 lines]
> Cheers,
> Martin
Jean-Guy Marcil - 12 Nov 2004 22:55 GMT
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
[quoted text clipped - 8 lines]
> 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
'_______________________________________

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
macropod - 13 Nov 2004 20:57 GMT
Hello Jean-Guy,
As with the other attempted abbreviated solutions, this doesn't deal with
leap years correctly. You need to get that part right before you can
extrapolate to months and days.
So, for an approximation starting with:
DayDiff = DateDiff("d", Date1, Date2)
Years could be calculated as:
INT((DayDiff+1)/365.2425)
Months could be calculated as:
INT((DayDiff+1-INT(Years*365.2425))/365.2425*12)
Days could be calculated as:
INT(DayDiff-INT(Years*365.2425+Months*365.2425/12))
Cheers
> macropod was telling us:
> macropod nous racontait que :
[quoted text clipped - 59 lines]
> End Sub
> '_______________________________________
Jean-Guy Marcil - 13 Nov 2004 22:59 GMT
macropod was telling us:
macropod nous racontait que :
>> How about this, using DateDiff to get a rough estimate? I say
>> "rough" because the weakness lies in the number of days per moth.
...and a total disregard for leap years!
Thanks!

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org