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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try

Code:
Range("I14").Value = DateDiff("yyyy", Date, Range("K9").Value)
 
Upvote 0
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?
 
Upvote 0
I had it the wrong way round. Reverse the 2nd and 3rd arguments,
 
Upvote 0
I'm sorry I couldn't understand. If you don't mind em asking; how shall I do that?
 
Upvote 0
This is not rocket science

Code:
Range("I14").Value = DateDiff("yyyy", Range("K9").Value, Date)
 
Upvote 0
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.
 
Upvote 0
Do you mean

Code:
range("I14").Value = DateDiff("yyyy", range("K9").Value, Date) & " years"
 
Upvote 0
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?
 
Upvote 0
Just put this formula in I14

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

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

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