Place age in cell

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444
The following code places the age in column "E" when the date of birth is written in column "D"

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 4 Then
        Target.Offset(, 1).Formula = "=IF(DATEDIF(RC[-1],NOW(),""y"")>0,DATEDIF(RC[-1],NOW(),""y"") & "" years"",IF(DATEDIF(RC[-1],NOW(),""m"")>0,DATEDIF(RC[-1],NOW(),""ym"") & "" months"",DATEDIF(RC[-1],NOW(),""y"")))"
    End If
End Sub
How could I change the code so that it places the age in cell "I14" when the date of birth is written in cell "K9"

Any help on this would be kindly appreciated.

Thanks in advance.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Range("I14").Value = DateDiff("yyyy", Date, Range("K9").Value)
 

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444
Thanks for the help. But the code as places the age with a minus figure. For example if the age is written as 16/1/1980 it places the age as -31. Instead of 31 Years.

How could this be overcome?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
I had it the wrong way round. Reverse the 2nd and 3rd arguments,
 

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444

ADVERTISEMENT

I'm sorry I couldn't understand. If you don't mind em asking; how shall I do that?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
This is not rocket science

Code:
Range("I14").Value = DateDiff("yyyy", Range("K9").Value, Date)
 

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444

ADVERTISEMENT

Thanks for the reply Peter. But your line of code is giving only the numerical value. Meaning it does not say 16 Years or 12 Months. Instead it gives 16 or 12.

How could the code be adjusted so that it gives age in years?

Any help on this would be kindly appreciated.

Note: I'm aware this is not rocket science but people do misunderstand and makes mistakes.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Do you mean

Code:
range("I14").Value = DateDiff("yyyy", range("K9").Value, Date) & " years"
 

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444
Yeah I did mean so. But if I write the date as 1/1/2011 the code is giving me the result as 0 Years instead of giving 2 Months. Also if write 1/3/2011 still it gives 0 Years instead of giving 8 days.

How could this be avoided. I mean calculate the age as either years, months or days individually?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Just put this formula in I14

=IF(K9="","",DATEDIF(K9,TODAY(),"y")&" y "&DATEDIF(K9,TODAY(),"ym")&" m "&DATEDIF(K9,TODAY(),"md") &" d")
 

Forum statistics

Threads
1,141,016
Messages
5,703,748
Members
421,312
Latest member
Mooncake1

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
Top