VBA & Macro Problems

How to Convert Years, Months, and Days into an Average Age in Access with VBA

Emma BrownEmma Brown Nov 13, 2026 870 views

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.

How to Convert Years, Months, and Days into an Average Age in Access with VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the Visual Basic Editor

In Microsoft Access, press Alt+F11 or go to Database Tools > Visual Basic to open the VBA editor.

2
Add a standard module

In the Project Explorer, right-click the database project and choose Insert > Module. Save the module with a name such as modAgeConversion.

3
Paste the VBA function

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

4
Create a query to calculate the average age

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.

Create a VBA Function to Convert Age Text into an Approximate Date
Result: The query returns the group's approximate average age in years. Use 365.25 to account for leap years over a multi-year group.
Free Microsoft Office alternative

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. 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. 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. 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.
Free, lightweight Office suite for daily spreadsheet and document workOpens and saves Microsoft Excel .xlsx and .xls files without losing age-calculation formulasFamiliar ribbon interface and VBA-style macro support in WPS SpreadsheetsBuilt-in PDF tools to export age reports and summaries
QA img-9

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.