In this lesson, we explore Error Handling, a critical skill for building professional macros that don’t crash when they encounter the unexpected. Since bugs are an inevitable part of development, learning how to “catch” them ensures your tools remain reliable and user-friendly.
Key Takeaways:
-
- Runtime Errors: These occur during execution (e.g., division by zero) and stop your code unless handled.
-
- On Error GoTo: Redirects the code to a specific “Handler” section when a bug occurs.
-
- The Err Object: Use Err.Number and Err.Description to identify and explain the specific problem.
-
- Exit Sub: Always place Exit Sub before your error handler to prevent it from running during normal execution.
-
- On Error Resume Next: Tells VBA to ignore the error and immediately move to the very next line of code.
-
- Clean Up: Use error handlers to ensure your macro closes files or turns settings back on even if it crashes.