How to Convert Years, Months, and Days into an Average Age in Access with VBA
Question details
The user needs to convert age values stored as text, such as "1Y 1M", into an approximate date or decimal age so the group average age can be calculated.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- The user maintains age data as text measurements and wants to estimate the average age in an Access query or in a worksheet.
- Observed behavior
- The text values cannot be averaged directly, so the year, month, and day portions must be parsed with a VBA function before averaging.
Open the Visual Basic Editor with Alt+F11 and confirm that the age values use a consistent Y, M, and D order, such as 1Y 1M or 1Y 1M 10D.
Create a VBA Function to Convert Age Text into an Approximate Date
Use this method when age data is stored as text in an Access table or query and you need a reusable VBA function.
The function reads the numbers before Y, M, and D, subtracts them from the current date, and returns an approximate birth date. Once the birth date is available, you can calculate each person's age and average the results.
In Microsoft Access, press Alt+F11 or go to Database Tools > Visual Basic to open the VBA editor.
In the Project Explorer, right-click the database project and choose Insert > Module. Save the module with a name such as modAgeConversion.
Add the following public function: Function AgeTextToDate(ageText As String) As Variant Dim y As Long, m As Long, d As Long, i As Long, n As String y = 0: m = 0: d = 0: n = "" For i = 1 To Len(ageText) If Mid(ageText, i, 1) Like "[0-9]" Then n = n & Mid(ageText, i, 1) Else Select Case UCase(Mid(ageText, i, 1)) Case "Y": y = Val(n): n = "" Case "M": m = Val(n): n = "" Case "D": d = Val(n): n = "" Case Else: n = "" End Select End If Next i AgeTextToDate = DateAdd("yyyy", -y, Date) AgeTextToDate = DateAdd("m", -m, AgeTextToDate) AgeTextToDate = DateAdd("d", -d, AgeTextToDate) End Function
Create a new query, add the table that contains the age text, and add this calculated column: AverageAgeYears: Avg(DateDiff("d", AgeTextToDate([AgeText]), Date()))/365.25 Replace [AgeText] with your actual field name.

Use WPS Office as a Free Alternative for Age Calculations and Spreadsheet Reporting
Because WPS Office does not include an Access replacement, this specific Access VBA task cannot be fixed inside WPS Office. WPS Office is a free, lightweight alternative for spreadsheet-based age calculations, with Excel-compatible workbooks, a familiar interface, and easy migration for reporting.
- 1. Download and install WPS Office: Go to the WPS Office download page and install the free desktop version for Windows, macOS, Linux, or mobile.
- 2. Open your Excel-compatible workbook: Open the workbook that contains your age data in WPS Spreadsheets, or create a new workbook and import the data.
- 3. Use formulas or macros for age calculations: Apply date formulas or VBA-style macros in WPS Spreadsheets to convert age text into average age, then save as .xlsx for compatibility.

Frequently Asked Questions
Can I use this VBA function in an Access query?
Yes. Put the function in a standard module as a Public Function, then call it in a query such as AgeTextToDate([AgeField]).
Why does the query return #Name? when I call the VBA function?
The function is usually missing or private. In Access, place it in a standard module and make sure the function name is spelled exactly in the query.
Can I run this Access VBA code in WPS Office?
WPS Office does not include Microsoft Access. You can use WPS Spreadsheets formulas or VBA-style macros to perform a similar date conversion in a workbook.
How do I handle age values that include days, such as 1Y 1M 10D?
The VBA function parses Y, M, and D tokens, so 1Y 1M 10D is converted by subtracting 1 year, 1 month, and 10 days from the current date.
Why divide by 365.25 when calculating average age?
Using 365.25 accounts for leap years and gives a more accurate average age in years when working with approximate dates.




