VBA macros · Utilities
VBA macro to add a timestamp when a cell changes in Excel
Write the date and time next to a cell automatically whenever someone edits it, using the Worksheet_Change event. Static, not recalculated like NOW().
A task list, a stock log or an order tracker becomes much more useful when every change is stamped with the time it happened. The NOW function cannot do this, because it recalculates and always shows the current time. This event macro watches column B and writes a fixed date and time into column C of the same row as soon as a value in B is entered or changed.
Option Explicit
' Put this code in the sheet's own module (right-click the tab > View Code)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim watched As Range, c As Range
' Only react to changes in B2:B10000
Set watched = Intersect(Target, Me.Range("B2:B10000"))
If watched Is Nothing Then Exit Sub
On Error GoTo SafeExit
Application.EnableEvents = False ' writing the stamp must not trigger this event again
For Each c In watched.Cells
If IsEmpty(c.Value) Then
c.Offset(0, 1).ClearContents ' value deleted: remove the stamp
Else
c.Offset(0, 1).Value = Now
c.Offset(0, 1).NumberFormat = "yyyy-mm-dd hh:mm"
End If
Next c
SafeExit:
Application.EnableEvents = True
End SubHow to use it
- Right-click the sheet tab that should get timestamps and choose View Code. The VBA editor opens with that sheet's own code module (for example Sheet1 (Orders)).
- Paste the code into that module. It must not go into a normal module from Insert › Module, because events only fire from the module of the object they belong to.
- Change
B2:B10000to the cells you want to watch. The stamp goes one column to the right (Offset(0, 1)). - Close the editor and type something in column B. The date and time appear in column C.
- Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
There is nothing to run with Alt+F8: an event macro starts by itself whenever its event happens. Events are explained step by step in Events: macros that run by themselves.
How it works
Worksheet_Change runs after any cell on the sheet is changed by typing, pasting, deleting or by code. Target is the range that changed; when someone pastes a block, it is the whole block. Intersect keeps only the part of Target that lies inside the watched area, and returns Nothing when the change happened elsewhere, in which case the macro stops immediately.
The most important line is Application.EnableEvents = False. Writing the timestamp is itself a change to the sheet; without switching events off, that write would trigger Worksheet_Change again. Here column C is outside the watched area, so it would stop after one extra call, but for a watched area that includes the stamp column it would loop forever. On Error GoTo SafeExit guarantees events are switched back on even if something fails; if they stay off, all event macros in Excel stop working until you restart it.
The loop handles pasted blocks cell by cell. Deleting a value clears its stamp, and every stamp gets a clear date-time number format. Now is evaluated once at that moment and stored as a plain value, so it never changes later.
Change it
Variations people often ask for:
- Stamp only the first entry and never update it: wrap the stamping in
If IsEmpty(c.Offset(0, 1).Value) Then ... End If. - Date only: use
Dateinstead ofNowand the format"yyyy-mm-dd". - Also record who made the change: add
c.Offset(0, 2).Value = Application.UserName. This is the name set in File › Options › General, which users can change, so it is not proof of identity. - Watch several columns:
Intersect(Target, Me.Range("B2:B10000, E2:E10000")), and write the stamp to a fixed column withMe.Cells(c.Row, "H").Value = Now. - Keep a log instead: write the sheet name, cell address, new value and time to the next free row of a Log sheet.
Without VBA, a circular-reference trick with iterative calculation can freeze a timestamp, but it is fragile and breaks easily. The event macro is the reliable way.
What can go wrong
Application.EnableEvents = True in the Immediate window (Ctrl+G) and press Enter, or restart Excel.Changes that are not edits, such as a formula in column B recalculating to a new result, do not fire Worksheet_Change. Sorting the sheet also does not change stamps, which move with their rows. Undo (Ctrl+Z) is not available after an event macro has changed the sheet, a side effect of every macro.
Related: Events: macros that run by themselves, Working with dates and times in VBA, NOW.