Formula output in the same cell as user input

Staceycat

New Member
Joined
Aug 7, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Please help! I am trying to display “age” in years calculated from a date of birth.

I want the user to input the date of birth and for this dob to then be converted to age, but displayed in the same cell. I need the formula to work across multiple sheets (in the same workbook) always in the same column.
 

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
so if the user enters his DoB in cell A1, you want his Age computed and displayed in same A1? if yes, is there a reason why the Age can't be displayed in A1?
 
Upvote 0
- Open VBE (Alt + F11)
- Under the Project pane on the left double click on the sheet name below where it says "MicrosoftExcelObjects"
- On the main big pane paste the following code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    On Error Resume Next
    If Target.Row = 1 And Target.Column = 1 Then 'cell A1
        Range("A1").Value = DateDiff("yyyy", Range("A1").Value, Now())
        Range("A1").NumberFormat = "General"
    End If
    Application.EnableEvents = True
End Sub
- Make sure to change the cell address wherever you see A1 to the proper cell address
- Also change Target.Row = 1 And Target.Column = 1 to the proper address as well. for example if you are using E2 then change it to Target.Row = 2 And Target.Column = 5 (E being the 5th column)
 
Upvote 0
- Open VBE (Alt + F11)
- Under the Project pane on the left double click on the sheet name below where it says "MicrosoftExcelObjects"
- On the main big pane paste the following code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    On Error Resume Next
    If Target.Row = 1 And Target.Column = 1 Then 'cell A1
        Range("A1").Value = DateDiff("yyyy", Range("A1").Value, Now())
        Range("A1").NumberFormat = "General"
    End If
    Application.EnableEvents = True
End Sub
- Make sure to change the cell address wherever you see A1 to the proper cell address
- Also change Target.Row = 1 And Target.Column = 1 to the proper address as well. for example if you are using E2 then change it to Target.Row = 2 And Target.Column = 5 (E being the 5th column)

Hi, thank you so much 🙏

I am new to VBE - will this work for a whole column? I have multiple rows of data, so can I have the “target row” as a range of cells and the target column as a whole column?
 
Upvote 0
@iggydarsa @Staceycat
The suggestion in post #3 does not work (consistently) for me, even in that single cell. For example 25 April 2000 correctly returns 23 but 25 November 2000 incorrectly also returns 23.

I need the formula to work across multiple sheets (in the same workbook) always in the same column.
Welcome to the MrExcel board!

So, suppose that is column A in any worksheet in the workbook, try this code after removing the previously suggested code.
Double-click on the ThisWorkbook module in the vba window and paste the code below on the resulting right hand pane.
This code would also process multiple dates if they were entered in column A (for example with Ctrl+Enter or by copy/paste from elsewhere)
If you only want this to work on limited worksheets within the workbook then please provide details (it would not matter if dates are not going to be input in column A of other sheets)

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  Dim Changed As Range, c As Range
  Dim DoB As Date

  Set Changed = Intersect(Target, Sh.Columns("A"))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    For Each c In Changed
      If IsDate(c.Value) Then
        DoB = c.Value
        c.Value = Year(Date) - Year(DoB) + (Date < DateSerial(Year(Date), Month(DoB), Day(DoB)))
        c.NumberFormat = "General"
      End If
    Next c
    Application.EnableEvents = True
  End If
End Sub
 
Last edited:
Upvote 0
@iggydarsa @Staceycat
The suggestion in post #3 does not work (consistently) for me, even in that single cell. For example 25 April 2000 correctly returns 23 but 25 November 2000 incorrectly also returns 23.


Welcome to the MrExcel board!

So, suppose that is column A in any worksheet in the workbook, try this code after removing the previously suggested code.
Double-click on the ThisWorkbook module in the vba window and paste the code below on the resulting right hand pane.
This code would also process multiple dates if they were entered in column A (for example with Ctrl+Enter or by copy/paste from elsewhere)
If you only want this to work on limited worksheets within the workbook then please provide details (it would not matter if dates are not going to be input in column A of other sheets)

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  Dim Changed As Range, c As Range
  Dim DoB As Date

  Set Changed = Intersect(Target, Sh.Columns("A"))
  If Not Changed Is Nothing Then
    Application.EnableEvents = False
    For Each c In Changed
      If IsDate(c.Value) Then
        DoB = c.Value
        c.Value = Year(Date) - Year(DoB) + (Date < DateSerial(Year(Date), Month(DoB), Day(DoB)))
        c.NumberFormat = "General"
      End If
    Next c
    Application.EnableEvents = True
  End If
End Sub
Hi Peter,

Thank you for the update, much appreciated. It has worked as required 🙂
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

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