Excel VBA: Run Macro When Cell Value Changes
In this article
Yes, you can run a macro automatically when a cell value changes in Excel using the Worksheet_Change event. The event fires whenever a user or code edits a cell, and you place your VBA code inside that event handler in the worksheet module. This guide walks you through setting it up, avoiding common pitfalls, and troubleshooting when it does not fire.
What You'll Need
- Excel for Windows or Mac (VBA works on both, but menu labels may differ slightly by version and region).
- A workbook saved as .xlsm (macro-enabled) or .xlsb. Standard .xlsx files cannot store macros.
- Basic familiarity with the VBA editor (Alt+F11 on Windows, Fn+Option+F11 on Mac).
- Macro security settings that allow macros to run. If macros are disabled, the event will not fire.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac). The editor opens in a separate window.
- Locate the worksheet module. In the Project Explorer (left pane), find your workbook, expand Microsoft Excel Objects, and double-click the sheet where the cell you want to monitor lives (for example, Sheet1). Do not use a standard module (Insert > Module) for event code; it will not fire.
- Choose the Worksheet_Change event. At the top of the code window, use the left dropdown to select Worksheet and the right dropdown to select Change. Excel inserts an empty subroutine:
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub - Limit the event to specific cells. Use Intersect to check whether the changed cell is the one you care about. For example, to react only when cell B2 changes:
If Not Intersect(Target, Me.Range("B2")) Is Nothing Then
Place your action inside that If block. Without this check, the macro runs on every edit in the sheet. - Add your action. Inside the If block, write the code you want to run. For example, to show a message:
MsgBox "B2 changed to " & Me.Range("B2").Value
Or to copy the value elsewhere:Me.Range("C2").Value = Me.Range("B2").Value - Guard against re-triggering. If your action changes another cell, the Change event fires again. Wrap the write in Application.EnableEvents = False and restore it afterward:
Application.EnableEvents = False
Me.Range("C2").Value = Me.Range("B2").Value
Application.EnableEvents = True
Always restore EnableEvents even if an error occurs; use an error handler if your code can fail. - Save and test. Close the editor, return to Excel, and edit B2. Your macro should run immediately. If nothing happens, check that macros are enabled and that you placed the code in the correct sheet module.
- Handle multiple cells if needed. If users can paste a range, Target may contain many cells. Loop through Target.Cells or use Target.Count to decide how to respond. For a single-cell check, add
If Target.Cells.Count > 1 Then Exit Subat the top.
Common Mistakes
- Putting the code in a standard module. Worksheet_Change must live in the worksheet's own module, not in Module1.
- Forgetting to disable events before writing. Writing to another cell inside the handler causes infinite loops or unexpected repeated runs.
- Not checking Intersect. The macro then runs on every edit, which is slow and can trigger unwanted actions.
- Saving as .xlsx. Macros are stripped. Save as .xlsm.
- Assuming it fires on formula recalc. Worksheet_Change fires on user or code edits, not when a formula result changes due to a dependency. Use Worksheet_Calculate for that, but it has no Target argument.
- Leaving EnableEvents off after an error. If your code errors before restoring it, events stay disabled until you reset them manually or reopen Excel.
Tips
- Use
Me.Rangeinstead ofActiveSheet.Rangeso the code always refers to its own sheet. - Keep the handler short; call a separate Sub for complex work to make debugging easier.
- Test with a copy of your workbook first, especially if the macro writes data.
- If you need to monitor several sheets, put a handler in each sheet module or use the Workbook_SheetChange event in the ThisWorkbook module.
- Use
Application.Undocautiously; it can interfere with the user's edit history.
Troubleshooting
If the macro does not run, work through these checks in order:
- Macros enabled? Check the yellow security bar or File > Options > Trust Center > Trust Center Settings > Macro Settings. Enable macros for the session or trust the file location.
- Correct module? Confirm the code is in the sheet module, not ThisWorkbook or a standard module.
- Events disabled? In the Immediate Window (Ctrl+G), type
Application.EnableEvents = Trueand press Enter. Then test again. - Intersect range wrong? Verify the cell address matches exactly, including the sheet. Use
Debug.Print Target.Addressto see what is firing. - Workbook protected? If the sheet is protected, some edits are blocked and the event may not fire as expected. Unprotect temporarily to test.
- Code errors? Add
On Error GoTo Handlerand a message box to surface the error, or step through with F8.
If none of that works, export the module, remove it, and re-import to rule out corruption. As a last resort, recreate the workbook in a fresh .xlsm file.
Frequently Asked Questions
Does Worksheet_Change fire when a formula recalculates?
No. It fires on user or code edits. Use Worksheet_Calculate to react to recalculated formula results, but it does not tell you which cell changed.
Can I monitor more than one cell?
Yes. Use Intersect against a multi-cell range, or check Target.Address. For pasted ranges, loop through Target.Cells.
Why does my macro run twice?
Your handler likely writes to another cell without disabling events. Wrap the write in Application.EnableEvents = False and restore it afterward.
Will this work on Excel for Mac?
Yes, VBA and Worksheet_Change work on Mac Excel, though keyboard shortcuts and some menu labels differ. Save as .xlsm.
Can I undo the change automatically?
You can call Application.Undo inside the handler, but it can conflict with the user's edit history and may not behave as expected. Test carefully.
Does it work in Excel Online?
No. Excel Online does not run VBA macros. Use the desktop app for macro-enabled workbooks.