ThisCalculator

Everyday

Excel Record Date and Time When Cell Changes

Updated · Standard formula, shown below

Yes, you can automatically record the date and time when a cell changes in Excel using a VBA macro with the Worksheet_Change event. This article explains how to set it up, how it works, and common pitfalls to avoid.

How the Worksheet_Change Event Works

Excel’s VBA (Visual Basic for Applications) includes an event called Worksheet_Change. This event fires every time a user changes a cell on a worksheet. By writing a small macro in the worksheet’s code module, you can capture the address of the changed cell and write the current date and time into a specified cell (for example, in a column next to the data). The macro runs automatically whenever a change occurs — no buttons, no manual steps.

Step-by-Step Setup

  1. Press Alt + F11 to open the VBA editor.
  2. In the Project Explorer (left pane), double-click the worksheet where you want the timestamp (e.g., Sheet1).
  3. In the code window, paste the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Application.EnableEvents = False
    For Each cell In Target
        If cell.Column = 1 Then ' Column A
            If cell.Value <> "" Then
                Cells(cell.Row, 2).Value = Now ' Column B
            Else
                Cells(cell.Row, 2).Value = ""
            End If
        End If
    Next cell
    Application.EnableEvents = True
End Sub

This code checks if the change is in column A; if yes, it writes the current date and time (using the Now function) into column B of the same row. If the cell is cleared, the timestamp is also cleared. Adjust column numbers to match your layout.

Why You Need to Disable Events

Inside the macro, Application.EnableEvents = False is critical. Without it, the macro would write to column B, which itself triggers the Worksheet_Change event, causing an infinite loop. After the macro finishes, events are re-enabled. Always include this safeguard.

Handling Multiple Cells and Undo

If a user pastes a range of cells, the Target can be a multi-cell range. The For Each loop handles that gracefully. However, note that using VBA to write values makes the Excel Undo (Ctrl+Z) not work for the timestamp. This is a known limitation — the user can undo their original change, but the timestamp remains. If you need full undo, you would need a more complex approach (e.g., using a formula with circular references or a third-party add-in).

Alternative: Using a Formula (Without VBA)

If you cannot use macros (e.g., file is .xlsx, not .xlsm, or macros are disabled), you can use a circular reference formula with iterative calculation enabled. For example, in cell B2, enter:

=IF(A2="","",IF(B2="",NOW(),B2))

Then enable iterative calculation via File → Options → Formulas → Enable iterative calculation. This formula writes the current time when column A is filled, and then keeps that value (because B2 is not empty). However, this method has quirks: it recalculates whenever the workbook recalculates, and it may not update if the cell is changed to a new value. It also requires the file to be saved as .xlsm (macro-enabled) if you use VBA; the formula method works in .xlsx.

Common Problems and Solutions

  • Macro does not run: Ensure the file is saved as .xlsm and macros are enabled (File → Options → Trust Center → Macro Settings).
  • Timestamp updates on every recalc: If using the formula method, the timestamp may change when the workbook recalculates. To prevent, use VBA instead.
  • Timestamp wrong when editing multiple cells: The VBA code above writes the time once per changed cell, but if you change a cell and then another, each gets its own timestamp — that’s correct.
  • Timestamp not updating when value is deleted: The code clears the timestamp if the cell is empty, which is often desired. If you want to keep the last timestamp, remove the Else clause.
  • VBA editor says “Ambiguous name detected”: You may have accidentally created two Worksheet_Change procedures. Delete one.

Limitations and Considerations

This method records the time of the change, not the time the workbook was last saved. If you need to know when a file was last modified, use Workbook_BeforeSave event instead. Also, the timestamp is static — it does not update if the cell is changed again unless the macro runs again. For auditing, you may want to log changes to a separate sheet with a history (including old and new values). That requires a more advanced macro that appends data to a log sheet.

Frequently Asked Questions

Can I record the date and time in a different column?

Yes, just change the column number in the code. For example, Cells(cell.Row, 3) writes to column C. The code checks the column of the changed cell; you can also check for a specific range (e.g., If Not Intersect(Target, Range("A1:A100")) Is Nothing).

Does the timestamp update when the cell is changed again?

Yes, the VBA macro overwrites the timestamp with the new current time every time the cell changes. If you want to keep the first timestamp only, add a condition: If Cells(cell.Row, 2).Value = "" Then before writing.

Will this work on Excel for Mac or Excel Online?

VBA macros work on Excel for Windows and Mac (desktop versions). Excel Online (browser) does not support VBA, so you would need to use the formula method or an Office Script (which is limited).

How do I make the timestamp update only when a specific cell changes?

Use If Target.Address = "$A$1" Then to restrict the macro to a single cell. For a range, use If Not Intersect(Target, Range("A1:A10")) Is Nothing Then.

Why does the timestamp show a date and time but not seconds?

The Now function includes seconds, but the cell format may be hiding them. Change the number format to dd/mm/yyyy hh:mm:ss to display seconds.

Can I record the username who made the change?

Yes, use Application.UserName in the macro. For example, write Cells(cell.Row, 3).Value = Application.UserName to capture the name in column C.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.