Handy VBA To Calculate Person Age

Posted on the September 10th, 2008 under Excel VBA Function by Poer @ Excel VBA/Macro

This handy VBA procedure is just to show you how we can calculate and display someone Age. To do this, of course we need to know their date of birth first, then simply using datediff and mod vba functions we can calculate how old is he/she.

Sub PersonAge()
    Dim iYears As Integer, iMonths As Integer, iMonth As Integer
    Dim dtDateofbirth As Date

    dtDateofbirth = _
    CDate(InputBox("Input your date of birth (yyyy-mm-dd)", "Date of birth"))

    iYears = DateDiff("yyyy", CDate(dtDateofbirth), Date)
    iMonths = (DateDiff("m", CDate(dtDateofbirth), Date)) Mod (iYears * 12)

    MsgBox "Your age are " & iYears & " year(s) and " & iMonths & " month(s)"
End Sub

For this example, I use MsgBox and InputBox to get input and display the output data, but we can change it to worksheet cells reference if we want to integrate this calculation in our Excel Worksheet.

Related Entries

External Resources

Tagged with:

Leave a Reply




XHTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
SYNTAX HIGHLIGHTER:
Place your VBA code between <pre> tags like this <pre class="brush:vb"> sub vba() ... end sub </pre>.