On Error
Controls how VBA responds when a run-time error occurs.
On Error GoTo Handler
...
Exit Sub
Handler:
' recoveryThis static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Controls how VBA responds when a run-time error occurs.
On Error is essential in VBA because workbook automation frequently touches fragile external state: missing sheets, missing files, locked workbooks, bad user input, and object model calls that raise run-time errors. It is powerful but dangerous. Structured handlers are usually safer than leaving Resume Next active across large blocks of code.
Quick reference
Syntax
On Error GoTo Handler
...
Exit Sub
Handler:
' recovery
See it in practice
Examples
Guard a worksheet lookup
On Error GoTo MissingSheet
Worksheets("Report").Activate
Exit Sub
MissingSheet:
MsgBox "Report sheet not found"Use a focused error path for expected workbook failures instead of crashing the macro.
Debug faster
Common Errors
Silent failure
Cause: Leaving On Error Resume Next active and masking unrelated failures later in the procedure.
Fix: Limit Resume Next to a small block and reset error handling with On Error GoTo 0 as soon as possible.
Runtime support
Compatibility
Common questions
Frequently Asked Questions
Controls how VBA responds when a run-time error occurs.
Silent failure: Limit Resume Next to a small block and reset error handling with On Error GoTo 0 as soon as possible.