Compute Age in Years and Months
Public Function GetAgeStr(varDOB As Variant, varDate As Variant) As String
' Purpose   Returns a string of the age between dates in the following format:
'           Xy-Ym   -  Where X is years and Y is the months between dates
' Author    Ron Weiner    rweiner@WorksRite.com
'           Copyrite © 2001-2005 WorksRite Software Solutions
'           You may use this code example for any purpose what-so-ever with
'           acknowledgement. However, you may not publish the code without
'           the express, written permission of the author.
    Dim dteDOB As Date, dteDate As Date
    Dim lngMonths As Long, lngYears As Long

    If IsDate(varDOB) And IsDate(varDate) Then
        dteDOB = CDate(varDOB)
        dteDate = CDate(varDate)
        lngMonths = DateDiff("M", dteDOB, dteDate) Mod 12
        lngYears = DateDiff("M", dteDOB, dteDate) \ 12
        If DatePart("m", dteDate) = DatePart("m", dteDOB) And DatePart("d", dteDate) < DatePart("d", dteDOB) Then
            lngYears = lngYears - 1
            lngMonths = 11
        End If
        GetAgeStr = lngYears & "y-" & lngMonths & "m"
    Else
        GetAgeStr = ""
    End If
End Function

 

Copyright © 2001 WorksRite Software Solutions -- Last Updated 06/13/05
Problems with this site? Please contact the Webmaster@WorksRite.com with your comments, questions, or suggestions.