Wauvel

Excel like a finance pro.

← The library

VBA · Events

Worksheet and workbook events

Code that runs automatically when something happens, like a cell changing or the workbook opening.

CommonDifficulty 1350 · Advanced
Practice · 2 questions →

When to use it

Event procedures run automatically when something happens: a cell changes, a sheet is selected, the workbook opens. They live in the sheet or ThisWorkbook module and have fixed names.

The code

Code
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("B:B")) Is Nothing Then ...
End Sub

Worked examples

  • React to a change

    Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("B:B")) Is Nothing Then MsgBox "B changed" End Sub Reacts only to edits in column B

    Intersect limits the trigger.

  • React to selection

    Private Sub Worksheet_SelectionChange(ByVal Target As Range) Application.StatusBar = Target.Address End Sub Shows the selected address in the status bar

    Selection events fire constantly; keep them light.

  • Block an action

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) If Range("A1").Value = "" Then Cancel = True End Sub Blocks saving until A1 is filled

    Cancel arguments stop the action.

Worth knowing

  • Event code lives in the sheet or ThisWorkbook module, not a standard module.
  • Pick the object and event from the two dropdowns at the top of the module to get the exact signature.
  • A Change event that writes to the sheet must disable events or it calls itself.
  • Events fire for code changes too, not just user edits.

Where it goes wrong

  • Writing to cells inside Worksheet_Change without EnableEvents = False causes an infinite loop.
  • Events do not run while macros are disabled or Application.EnableEvents is False.

Related

Learn the moves here — or let Wauvel run them on your numbers.

Meet your AI CFO →

One CFO-grade Excel tip a week

A short, practical email for finance operators — functions, shortcuts, and the moves that save an afternoon. Free, unsubscribe anytime.