Advanced age display from birthday

Marrows

New Member
Joined
Nov 25, 2013
Messages
2
Hi I work on a paediatric ward in a hospital and my boss has set me a challenge for our handover sheet.
If I enter the birthday of a child, I can have the age displayed as 1y10m6d for eg.
What we want is if the child is younger than 1 to have just 2m5d displayed for eg.
If aged 1-3 have 1y4m displayed for eg.
and older than 3 just have 6y displayed for eg.


Is this possible? I have no idea where to start!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Hi I work on a paediatric ward in a hospital and my boss has set me a challenge for our handover sheet.
If I enter the birthday of a child, I can have the age displayed as 1y10m6d for eg.
What we want is if the child is younger than 1 to have just 2m5d displayed for eg.
If aged 1-3 have 1y4m displayed for eg.
and older than 3 just have 6y displayed for eg.

Is this possible? I have no idea where to start!

Give this UDF (user defined function) a try...

Code:
Function YMD(ByVal StartDate As Date, _
             Optional ByVal EndDate As Variant, _
             Optional LeapDayInNonLeapYearIsMar1 As Boolean = True) As String
  Dim TempDate As Date, NumOfHMS As Double
  Dim NumOfYears As Long, NumOfMonths As Long, NumOfWeeks As Long, NumOfDays As Long
  StartDate = Int(StartDate)
  If IsMissing(EndDate) Then
    Application.Volatile
    EndDate = Date
  Else
    EndDate = Int(EndDate)
  End If
  If Not LeapDayInNonLeapYearIsMar1 And IsDate("2/29/" & Year(StartDate)) Then
    If Format(StartDate, "m/d") = "2/29" Then StartDate = StartDate - 1
  End If
  NumOfYears = DateDiff("yyyy", StartDate, EndDate)
  StartDate = DateSerial(Year(EndDate), Month(StartDate), Day(StartDate))
  If StartDate > EndDate Then
     StartDate = DateAdd("yyyy", -1, StartDate)
     NumOfYears = NumOfYears - 1
  End If
  NumOfMonths = DateDiff("m", StartDate, EndDate)
  StartDate = DateSerial(Year(EndDate), Month(EndDate), Day(StartDate))
  If StartDate > EndDate Then
     StartDate = DateAdd("m", -1, StartDate)
     NumOfMonths = NumOfMonths - 1
  End If
  NumOfDays = Abs(DateDiff("d", StartDate, EndDate))
  If NumOfYears < 1 Then
    YMD = CStr(NumOfMonths) & "m" & CStr(NumOfDays) & "d"
  ElseIf NumOfYears < 4 Then
    YMD = CStr(NumOfYears) & "y" & CStr(NumOfMonths) & "m"
  Else
    YMD = CStr(NumOfYears) & "y"
  End If
End Function

HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use YMD just like it was a built-in Excel function. For example,


=YMD(A1,A2)
 
Upvote 0
THANKYOU THANKYOU THANKYOU!!! My husband is very good with Excel and even he can't believe this could be done! It works perfectly!
Cheers, Marie :)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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