Excel VBA Worksheet Events: Step-by-Step Guide
In this article
Worksheet events in Excel VBA are automatic macros that run when something happens on a specific sheet — a cell changes, a selection moves, a double-click lands, or the sheet is activated. To use them, open the Visual Basic Editor (Alt+F11), double-click the sheet's module in the Project Explorer, choose the event from the right-hand dropdown, and write your code inside the generated procedure. The code must live in that sheet's own module, not in a standard module, or it will never fire.
What You'll Need
- Excel for Windows or Mac with macros enabled (the Developer tab visible: File > Options > Customize Ribbon > check Developer, or Excel > Preferences on Mac).
- A workbook saved as .xlsm (macro-enabled) so your event code survives closing and reopening.
- Macro security set to allow macros, at least for this workbook: File > Options > Trust Center > Trust Center Settings > Macro Settings. Labels vary slightly by Excel version and platform.
- A basic understanding of writing a Sub and referring to ranges, e.g. Range("A1").Value.
Step-by-Step
- Save the workbook as .xlsm first. Use File > Save As and pick Excel Macro-Enabled Workbook. If you skip this, Excel will warn you when you close and your event code will be lost.
- Open the Visual Basic Editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or the Developer tab > Visual Basic). The Project Explorer pane on the left lists your workbook, its sheets, and any modules.
- Find the correct sheet module. In the Project Explorer, expand VBAProject, then Microsoft Excel Objects. Double-click the entry for the sheet you want, for example Sheet1 (Sales). This opens that sheet's code module — the only place worksheet events work.
- Pick the event from the dropdowns. At the top of the code window are two dropdowns. The left one should show Worksheet. Open the right-hand dropdown and choose the event — for example Change, SelectionChange, BeforeDoubleClick, Activate, or BeforeRightClick. The VBE inserts an empty procedure such as Private Sub Worksheet_Change(ByVal Target As Range) with the correct signature.
- Write code inside the procedure. Do not rename the procedure or change its arguments. A simple example: inside Worksheet_Change, type If Not Intersect(Target, Me.Range("B2:B100")) Is Nothing Then MsgBox "You changed a tracked cell." This only reacts to edits in B2:B100.
- Guard against re-triggering. If your event code writes to a cell, it fires the event again. Wrap the writing part with Application.EnableEvents = False at the start and Application.EnableEvents = True at the end, and restore it even if an error occurs (use an error handler or On Error Resume Next carefully). Forgetting this is the most common cause of runaway loops.
- Test it. Switch back to Excel (Alt+F11 toggles), edit a cell in the watched range, and confirm the behaviour. If nothing happens, check that the code is in the sheet module, not a standard module, and that events are enabled.
- Add more events the same way. Return to the sheet module, choose another event from the right-hand dropdown, and fill in the new procedure. Each sheet has its own set of events, so code on Sheet1 does not run for edits on Sheet2.
Common Mistakes
- Putting the code in a standard module. Worksheet events only fire from the sheet's own module. A Sub named Worksheet_Change in Module1 is just an ordinary macro and never runs automatically.
- Leaving EnableEvents turned off. If your code crashes after setting Application.EnableEvents = False, events stay disabled for the whole session. In the Immediate Window (Ctrl+G) type Application.EnableEvents = True and press Enter to restore them.
- Ignoring Target's size. Target can be many cells after a paste. Code that assumes a single cell, like Target.Value = UCase(Target.Value), errors on multi-cell ranges. Loop over Target.Cells or test Target.Count first.
- Using MsgBox in SelectionChange. It fires on every arrow-key press and makes the sheet unusable. Keep selection-event code lightweight.
- Renaming the procedure. Changing Worksheet_Change to something else silently breaks the link to the event.
- Forgetting the .xlsm format. Saving as .xlsx strips the VBA project and your events disappear.
Tips
- Use Me.Range(...) inside a sheet module to refer to that sheet without hard-coding its name.
- Use Intersect to limit an event to specific cells and keep performance reasonable on large sheets.
- Worksheet_Change gives you Target (what changed); Worksheet_SelectionChange gives you Target (what is now selected). They are not interchangeable.
- BeforeDoubleClick and BeforeRightClick receive a Cancel argument — set Cancel = True to stop Excel's default behaviour.
- Keep event code short and call ordinary Subs in a standard module for the heavy lifting; it is easier to test and reuse.
- Add Option Explicit at the top of the module to catch undeclared variables early.
Troubleshooting
If your event does not fire, work through these in order. First, confirm the code sits in the sheet module (Project Explorer > Microsoft Excel Objects > the sheet), not in ThisWorkbook or a standard module. Second, check that events are enabled: in the Immediate Window run ?Application.EnableEvents — it should return True. Third, confirm macros are allowed for the file and the workbook is .xlsm. Fourth, check whether your Intersect test is excluding the cell you are editing. Fifth, look for a compile error — a red-highlighted line stops the whole module from running. If the event fires but behaves oddly, suspect a re-trigger loop and add the EnableEvents guard. If the code works on one sheet but not another, remember each sheet has its own module and its own procedures.
Frequently Asked Questions
Do worksheet events work in Excel for the web?
No. VBA, including worksheet events, is not supported in Excel for the web. Open the file in the desktop app to run them.
What is the difference between Worksheet_Change and Workbook_SheetChange?
Worksheet_Change lives in one sheet's module and only fires for that sheet. Workbook_SheetChange lives in the ThisWorkbook module and fires for any sheet, with a Sh parameter telling you which one.
Can I undo an action from a worksheet event?
Not reliably. Application.Undo inside an event can behave unpredictably. A common pattern is to store the previous value before the change and restore it in the event code instead.
Why does my event run twice?
Usually because the event code changes a cell, which fires the event again. Disable events around the write, or restrict the code with Intersect so it does not react to its own edit.
Do events run when a macro changes cells?
Yes, unless Application.EnableEvents is False at the time. That is why well-written macros that write to cells switch events off first.
Can I copy event code between sheets?
Yes — copy the procedure text into the other sheet's module. The procedure name stays the same, but it only affects the sheet whose module contains it.