Handy VBA To Calculate Person Age
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
- Microsoft Excel 2003/2007 Video Tutorials
Step-by-step video guide to mastering Charts, PivotTable, Data Analysis and Macro programming in Microsoft Excel in 5 hours.
- 101 Secrets of Microsoft Excel
Discover 101 of Excels little-known secrets that have been hiding right under your nose.
Tagged with: datediff, mod, vba function