Use Cell Instead of Column

adamsm

Active Member
Joined
Apr 20, 2010
Messages
444
Hi the following code calculates age as date of birth is entered in column D and puts date on column E.

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 make the code so that when the user places the date in cell M12, the age gets calculated in cell I14?

Any help would be kindly appreciated.
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Will the DoB always be entered in M12? If so then:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$M$12" Then
        Range("I14").Formula = "=IF(DATEDIF(M12,NOW(),""y"")>0,DATEDIF(M12,NOW(),""y"") & "" years"",IF(M12,NOW(),""m"")>0,DATEDIF(M12,NOW(),""ym"") & "" months"",DATEDIF(M12,NOW(),""y"")))"
    End If
End Sub
 
Upvote 0
Yes date of birth will M12 but im getting the debug message highlighting the line

Range("I14").Formula = "=IF(DATEDIF(M12,NOW(),""y"")>0,DATEDIF(M12,NOW(),""y"") & "" years"",IF(M12,NOW(),""m"")>0,DATEDIF(M12,NOW(),""ym"") & "" months"",DATEDIF(M12,NOW(),""y"")))"
</pre>why is this?
 
Upvote 0
Do you just want the age or do you need the formula?
 
Upvote 0
I managed to delete part of your formula - it should have been:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$M$12" Then
        Range("I14").Formula = "=IF(DATEDIF(M12,NOW(),""y"")>0,DATEDIF(M12,NOW(),""y"") & "" years"",IF(DATEDIF(M12,NOW(),""m"")>0,DATEDIF(M12,NOW(),""ym"") & "" months"",DATEDIF(M12,NOW(),""y"")))"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

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