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

harzer

Board Regular
Joined
Dec 15, 2021
Messages
159
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
 
Uniquement pour en avoir le coeur net ...
Est ce que la fonction corrigée ci-dessous fonctionne correctement pour le test de la cellule L2
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(),""md"")&"" j """)
End Function
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
@Fluff You are 100 % right !!!

To be on the safe side...
Is the revised UDF working properly for cell L2 ...
 
Upvote 0
The revised UDF does not work either, I have the following results: #VALUE!
 
Upvote 0
So, if you would just recap :

1. The UDF does not work
and
2. The exact same formula =DATEDIF(H2,TODAY(),"y")&" a "&DATEDIF(H2,TODAY(),"ym")&" m "&DATEDIF(H2,TODAY(),"md")&" j "
does produce the L2 result : 65 a 10 m 14 j

Is that right ?
 
Upvote 0
Could it be just the French version of Excel ...?
What about testing the following local adjustment :
VBA Code:
=DATEDIF(H2;AUJOURDHUI();"y")&" a "&DATEDIF(H2;AUJOURDHUI();"ym")&" m "&DATEDIF(H2;AUJOURDHUI(),"md")&" j "
 
Upvote 0
Hello,
the formula :
=DATEDIF(H2,TODAY(),"y")&" a "&DATEDIF(H2,TODAY(),"ym")&" m "&DATEDIF(H2,TODAY(),"md")&" j " ---> Excel tells me that there is an error in the formula.
And with the formula :
=DATEDIF(H2;AUJOURDHUI();"y")&" a "&DATEDIF(H2;AUJOURDHUI();"ym")&" m "&DATEDIF(H2;AUJOURDHUI(),"md")&" j " ---> Excel shows me the same message that there is an error in the formula.
I propose something to you, I managed to make a code that gives me the desired result (to be linked with a button) but it is not fast enough in my real file, here is the code:
VBA Code:
Sub CalculAge()
    Application.ScreenUpdating = False
    Dim Y As Integer, j As Integer
    Dim M As Integer
    Dim D As Integer
    Dim Temp1 As Date
   

    With Sheets("Parents")
            lastRow = .Cells(.Rows.Count, "H").End(xlUp).Row                                                                                                           '#

            For j = 2 To lastRow                     'boucle sur les lignes : de la 2e ligne jusqu'à la dernière

                    Date1 = .Cells(j, "H").Value     'on met la date de naissance (Colonne "H") dans "Date"
                    Date2 = Date

                    Temp1 = DateSerial(Year(Date2), Month(Date1), Day(Date1))
                    Y = Year(Date2) - Year(Date1) + (Temp1 > Date2)
                    M = Month(Date2) - Month(Date1) - (12 * (Temp1 > Date2))
                    D = Day(Date2) - Day(Date1)
   
                            If D < 0 Then
                                    M = M - 1
                                    D = Day(DateSerial(Year(Date2), Month(Date2) + 1, 0)) + D + 1
                            End If

                    Age = Y & " a " & M & " m " & D & " j"
                    .Cells(j, "E").Value = Age
            Next j                                                                                                                                                   '#
    End With
    Application.ScreenUpdating = True
End Sub

Can you modify it to be processed by arrays to speed up the execution speed of the Macro.
I await an answer from both of you.
Thanks in advance.
 
Upvote 0
I forgot to specify to mention that I was inspired by a code resembling my needs.
 
Upvote 0
This is a straight conversion of your code from updating 1 cell at a time to using an array.

VBA Code:
Sub CalculAge()
    Application.ScreenUpdating = False
    Dim Y As Integer, j As Integer
    Dim M As Integer
    Dim D As Integer
    Dim Temp1 As Date
    Dim wsParents As Worksheet
    Dim rngDate As Range
    Dim arrDate As Variant
    Dim lastRow As Long
    Dim Date1 As Long, DateCurrent As Long
    
    Set wsParents = Worksheets("Parents")
    With wsParents
            lastRow = .Cells(.Rows.Count, "H").End(xlUp).Row                                 '#
            Set rngDate = .Range(.Cells(2, "H"), .Cells(lastRow, "H"))
            arrDate = rngDate.Value2
            ReDim Preserve arrDate(1 To UBound(arrDate, 1), 1 To 2)
    End With
            
    DateCurrent = CLng(Date)
    For j = 1 To UBound(arrDate)                        'boucle sur les lignes : de la 2e ligne jusqu'à la dernière

            Date1 = arrDate(j, 1)               'on met la date de naissance (Colonne "H") dans "Date"

            Temp1 = DateSerial(Year(DateCurrent), Month(Date1), Day(Date1))
            Y = Year(DateCurrent) - Year(Date1) + (Temp1 > DateCurrent)
            M = Month(DateCurrent) - Month(Date1) - (12 * (Temp1 > DateCurrent))
            D = Day(DateCurrent) - Day(Date1)

                    If D < 0 Then
                            M = M - 1
                            D = Day(DateSerial(Year(DateCurrent), Month(DateCurrent) + 1, 0)) + D + 1
                    End If

            arrDate(j, 2) = Y & " a " & M & " m " & D & " j"
    Next j                                                                                                                                                   '#

    rngDate.Offset(, -3).Value2 = Application.Index(arrDate, 0, 2)
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
As far as speed is concerned
VBA Code:
Sub CalcAges()
Dim i As Long, lr As Long
Dim arr()
lr = ActiveSheet.Range("H" & Rows.Count).End(xlUp).Row
ReDim arr(0 To lr - 2)
' Populate Array with Evaluate
    For i = 0 To lr - 2
        arr(i) = Evaluate("=DATEDIF(" & CLng(Cells(i + 2, 8).Value) & ",TODAY(),""y"")&"" a ""&DATEDIF(" & CLng(Cells(i + 2, 8).Value) & ",TODAY(),""ym"")&"" m ""&DATEDIF(" & CLng(Cells(i + 2, 8).Value) & ",TODAY(),""md"")&"" j """)
    Next i
' Display Results
ActiveSheet.Range("L2").Resize(UBound(arr) + 1).Value = Application.Transpose(arr)
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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