Excel like a finance pro.
VBA · Errors & debugging
On Error handling
Decides what happens when a runtime error occurs: jump to a handler, or ignore and continue.
When to use it
On Error decides what happens when a runtime error occurs: GoTo a handler label, Resume Next to ignore and continue, or GoTo 0 to restore normal behavior.
The code
- Code
On Error GoTo Handler ... Exit Sub Handler: MsgBox Err.Description
Worked examples
Handler
On Error GoTo Handler ... Exit Sub Handler: MsgBox Err.Description → Errors jump to the handler; Exit Sub keeps normal runs out of it
The standard pattern.
Test existence
On Error Resume Next Set ws = Worksheets("Maybe") On Error GoTo 0 If ws Is Nothing Then MsgBox "missing" → Tests for a sheet without crashing, then restores
Narrow use of Resume Next.
Cleanup
Handler: Application.ScreenUpdating = True MsgBox Err.Number & ": " & Err.Description → Cleanup plus a message
Restore settings in the handler.
Worth knowing
- On Error Resume Next hides every error that follows; reset with On Error GoTo 0 as soon as possible.
- On Error Resume Next hides every later error; turn it off with On Error GoTo 0 as soon as possible.
- Resume Next in a handler continues after the failing line; Resume retries it.
- Err.Clear resets after handling inside Resume Next blocks.
Where it goes wrong
- A handler without Exit Sub above it runs on every normal execution.
- Errors inside the handler itself are unhandled.
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.