Home » excel » excel – I want to enter todays date in cell Z2 if any cell between A2 and Y2 are altered

excel – I want to enter todays date in cell Z2 if any cell between A2 and Y2 are altered

Posted by: admin May 14, 2020 Leave a comment

Questions:

This is for recording the date of alteration for a record in a spreadsheet containing many thousand records. I appreciate any suggestions you may have; I can think of no way to do this with a formula, but if it is possible, I prefer it over a macro.

Thank You.

How to&Answers:

Although you specify only row 2 in your question, you mention thousands of rows, so I assume you want the macro to work for all rows, not just row 2. The following will add a date stamp to column Z in the row where the change occurred. It also works if multiple cells are changed at the same time, for example with a paste or delete operation.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

Dim cel As Range

If Not Intersect(Target, [A:Y]) Is Nothing Then
    On Error Resume Next
    Application.EnableEvents = False
    For Each cel In Target
        Range("Z" & cel.Row).Value = Date
    Next cel
    Application.EnableEvents = True
End If

End Sub

To install, right-click the sheet tab, click View Code and paste the above into the code window.

Answer:

Add this in to the code of the sheet where you have the values:

EDITED:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim KeyCells As Range
Set KeyCells = Range("A2:Y2")

If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Range("Z2").Value = Date
End If

End Sub

This way once any of the cells in range between A2 & Y2 changes, the value of Z2 will be updated with today’s date.