AGE
Returns the age of a person given a birthday.
Remarks
* For instructions on how to add a function to a workbook refer to the page under Inserting Functions
* The equivalent JavaScript function is AGE
If "dtBirthday" is not a valid date , then #VALUE! is returned.
Alternatively you could use the following formula: =INT(YEARFRAC(TODAY(),"01/07/1977",1))
'dtBirthday - The date of the birthday.
Public Function AGE(ByVal dtBirthday As Date) As Integer
If Int(dtBirthday) > 0 Then
Select Case Month(Date())
Case Is < Month(dtBirthday)
AGE = Year(Date()) - Year(dtBirthday) - 1
Case Is = Month(dtBirthday)
If Day(Date) >= Day(dtBirthday) Then
AGE = Year(Date()) - Year(dtBirthday)
Else
AGE = Year(Date()) - Year(dtBirthday) - 1
End If
Case Is > Month(dtBirthday)
AGE = Year(Date()) - Year(dtBirthday)
End Select
Else
AGE = CVErr(xlErrNA)
End If
End Function
Function CalcAge(dteBirthdate As Date) As Long
Dim lngAge As Long
' Make sure passed-in value is a date.
If Not IsDate(dteBirthdate) Then
dteBirthdate = Date
End If
' Make sure birthdate is not in the future.
' If it is, use today's date.
If dteBirthdate > Date Then
dteBirthdate = Date
End If
' Calculate the difference in years between today and birthdate.
lngAge = DateDiff("yyyy", dteBirthdate, Date)
' If birthdate has not occurred this year, subtract 1 from age.
If DateSerial(Year(Date), Month(dteBirthdate), Day(dteBirthdate)) > Date Then
lngAge = lngAge - 1
End If
CalcAge = lngAge
End Function
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext