Age calculation in Text Format, in year, month and day.

harzer

Board Regular
Joined
Dec 15, 2021
Messages
122
Office Version
  1. 2016
Platform
  1. Windows
I am facing a problem despite my research on the net, I confess that I can not find it.
Namely that I am unable to handle array variables correctly, which complicates my task.

I have a large amount of row data, in 10 columns, the ones that concern the present case, are columns E and H.
for now, column E is empty.
Column H contains dates of birth.
How to proceed, either by table or table combined with dictionary to indicate the age in: year, month and days in column E, according to the current date.
The goal is to have the result in text format, (Example: 15y 5m 10d), I thought of making a call to a function which would give the result just in text so as not to use formulas (No formulas) Please .
Unless I am mistaken, the desired result for the first 10 lines is in column L, based on the date that the calculation was made is: 30-01-2023
Thank you very much in advance for your suggestions.
PS: I completely emptied my table and I simply left the two columns concerned so that you can (eventually) test your code.
CalculAge1.xlsm
ABCDEFGHIJKLM
1AgeNé(e)Age
216-03-195765 a 10 m 14 j
317-04-201210 a 9 m 13 j
414-04-201210 a 9 m 16 j
525-03-196656 a 10 m 5 j
630-05-195963 a 8 m 0 j
717-02-196260 a 11 m 13 j
822-01-195568 a 0 m 8 j
915-02-196161 a 11 m 15 j
1018-04-201210 a 9 m 12 j
111-01-201013 a 0 m 29 j
1222-03-2019
1315-04-2007
1415-04-2007
1515-04-2007
1616-04-2007
1715-04-2008
1815-04-2008
1915-04-2008
2015-04-2008
2115-04-2008
2215-04-2008
2315-04-2008
2415-05-2008
2515-04-2008
Parents
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi,

You can test
=DATEDIF(H2,TODAY(),"y")&" a "&DATEDIF(H2,TODAY(),"ym")&" m "&DATEDIF(H2,TODAY(),"yd")&" j "
 
Upvote 0
Hi again,

From your comment, it would appear you need an UDF for the complete Age ...
VBA Code:
Function Age(rng As Range)
' Dans la cellule L2 saisir = age(H2)
  Age = Evaluate("=DATEDIF(" & CLng(rng.Value) & ",TODAY(),""y"")&"" a ""&DATEDIF(" & CLng(rng.Value) & ",TODAY(),""ym"")&"" m ""&DATEDIF(" & CLng(rng.Value) & ",TODAY(),""yd"")&"" j """)
End Function
 
Upvote 0
If you truly don't want a formula in the cells in column E, you can use Evaluate in VBA to produce the same results as the DATEDIF worksheet function, e.g.
VBA Code:
Sub harzer()
Dim i As Long, lastrow As Long
lastrow = Range("H" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow
    Cells(i, 5).Value = _
        Evaluate("DATEDIF(""" & Cells(i, 8).Value & """,""" & Date & """,""Y"")") & " y " & _
        Evaluate("DATEDIF(""" & Cells(i, 8).Value & """,""" & Date & """,""YM"")") & " m " & _
        Evaluate("DATEDIF(""" & Cells(i, 8).Value & """,""" & Date & """,""MD"")") & " d"
Next i
End Sub
You could adjust this code to go in the Worksheet_Change event, so it gets triggered anytime you enter a value in column H. If you ran the code above, it would generate the age for all values in column H into column E. You may also want to account for adjusting the values when opening the workbook (in the Workbook_Open event) so that it's always current.
 
Upvote 0
Hello
I am very happy that you have understood my request, I was afraid of not explaining the problem well.
Thank you for your quick response, however, as my file is very very heavy, I took the liberty of asking you to avoid formulas which only make the execution of my Marcos heavier and above all slow down.
Can you (please) try using arrays instead of formulas.
I know it will take longer to code, it will be the challenge for connoisseurs like you to help novices like me.
Thank you so much.
 
Upvote 0
Hello Z51,
Thank you for your reply.
I tested your code but unfortunately I got runtime error '13' type mismatch.
 
Upvote 0
Hello James006,
I am answering to tell you that there is a problem with the number of days. for example for the date of 16-03-1957, I must have: 65 a 10 m 14 d but I have: 65 a 10 m 320 d .
Thank you so much.
 
Upvote 0
Make sure your "dates" in column H are actual dates and not text representations of dates. If you format the cells as numbers they should show you 5-digit numeric values probably in the range of 18264 to 44956, which are Jan 1, 1950 to today.

My code works fine for me as provided (against your data that I copied from your original post). I added the CLng function below, like James used in his code, but don't see why that would make any difference. They both work the same.
VBA Code:
Sub harzer()
Dim i As Long, lastrow As Long
lastrow = Range("H" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow
    Cells(i, 5).Value = _
        Evaluate("DATEDIF(""" & CLng(Cells(i, 8).Value) & """,""" & Date & """,""Y"")") & " y " & _
        Evaluate("DATEDIF(""" & CLng(Cells(i, 8).Value) & """,""" & Date & """,""YM"")") & " m " & _
        Evaluate("DATEDIF(""" & CLng(Cells(i, 8).Value) & """,""" & Date & """,""MD"")") & " d"
Next i
End Sub
 
Upvote 0
Hello Z51,
I'm sorry but I still have the same error and yet my column H and in date format, see attached photo, Do you want me to send you my little file as an attachment to your address?
For your information, I must inform you that I have the version of Excel 2016 French version. Would there be a conflict at this level?
 

Attachments

  • Format date.jpg
    Format date.jpg
    142.3 KB · Views: 12
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,545
Members
449,317
Latest member
chingiloum

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top