Forum Discussion
Inserting a date in a cell when another has a value entered
- Oct 29, 2020
The problem with a formula involving TODAY() or NOW() will be updated continually. You need VBA code instead.
Let's say that you want to add a timestamp in column H when the user enters or changes a value in column D, starting in row 2.
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module:
Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range If Not Intersect(Range("D2:D" & Rows.Count), Target) Is Nothing Then Application.ScreenUpdating = False Application.EnableEvents = False For Each rng In Intersect(Range("D2:D" & Rows.Count), Target) If rng.Value = "" Then Range("H" & rng.Row).ClearContents Else Range("H" & rng.Row).Value = Now End If Next rng Application.EnableEvents = True Application.ScreenUpdating = True End If End Sub
Close the Visual Basic Editor.
Save the workbook as a macro-enabled workbook (.xlsm).
Make sure that you allow macros when you open the workbook.
The problem with a formula involving TODAY() or NOW() will be updated continually. You need VBA code instead.
Let's say that you want to add a timestamp in column H when the user enters or changes a value in column D, starting in row 2.
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
If Not Intersect(Range("D2:D" & Rows.Count), Target) Is Nothing Then
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each rng In Intersect(Range("D2:D" & Rows.Count), Target)
If rng.Value = "" Then
Range("H" & rng.Row).ClearContents
Else
Range("H" & rng.Row).Value = Now
End If
Next rng
Application.EnableEvents = True
Application.ScreenUpdating = True
End If
End Sub
Close the Visual Basic Editor.
Save the workbook as a macro-enabled workbook (.xlsm).
Make sure that you allow macros when you open the workbook.
- Kathy_WoodgatesOct 29, 2020Copper Contributor
Thank you so much. I did encounter this issue with TODAY and NOW. But by inserting this code it worked immediately once I referenced the cells I wanted it to affect.
Very much appreciated.