Why handle errors?
In VBA, if an error occurs (like trying to open a file that doesn't exist), the whole macro crashes and shows a scary "Debug" window to the user. Error Handling allows you to catch these problems and handle them gracefully.
1. On Error GoTo
This is the standard way to handle errors in VBA. You tell the code to jump to a specific label if something goes wrong.
vba
2. On Error Resume Next
Wait! This command tells VBA to ignore the error and just move to the next line. Use this very sparingly—only when you expect an error and know how to check for it immediately.
vba
The Err Object
When an error happens, the
Err object contains all the details:Err.Number: The numeric ID of the error.Err.Description: The English explanation of what went wrong.
Summary
| Term | Description |
|---|---|
| On Error | Instruction to start error handling |
| GoTo Label | Jumping to a specific handler block |
| Resume Next | Ignoring errors (Dangerous!) |
| Err.Clear | Resetting the error state |
| Exit Sub | Preventing the handler from running accidentally |
| Best For | Building professional and stable applications |
| Logic | "Anticipate failure and recover gracefully" |
| Safety | Prevents the user from seeing the code editor |
| Modern | Essential for robust automation |
| Standard | Use a consistent error handler in every major Sub |
| Identity | The difference between a "Script" and an "Application" |