Age Calculation
Here are two VBA Age Calculation examples for a solution to calculating the age
of something, a person for example, given a beginning date. Below are
two solutions using birth date and today's date.
Example 1 - Age Calculation Long Method
Private Sub Birth_Dte_AfterUpdate()
Dim iyears As Integer
Dim tmonth As Integer
Dim Bmonth As Integer
'
iyears = DateDiff("yyyy", "01/01/" & DatePart("yyyy",
Me.Birth_Dte), _
"01/01/" & DatePart("yyyy", Me.Todays_Date)) - 1
tmonth = DatePart("m", Me.Todays_Date)
Bmonth = DatePart("m", Me.Birth_Dte)
'
Select Case tmonth
Case Is > Bmonth
iyears =
iyears + 1
Case Is = Bmonth
If
DatePart("d", Me.Birth_Dte) <= _
DatePart("d",
Me.Todays_Date) Then iyears = iyears + 1
End Select
End Sub
Example 2 - VBA Age Calculation - Short Method Using Date Serial Visual
Basic Function:
Function age(vb_Dte As Variant)
Dim v_Age_Calc As Variant
v_Age_Calc = DateDiff("yyyy", vb_Dte, Now)
If Date < DateSerial(Year(Now), _
Month(vB_Date), Day(vb_Dte)) Then v_Age_Calc = v_Age_Calc - 1
age = CInt(v_Age_Calc)
End Function