Ever received an error message popping up from an application you had no idea what it means, what to do and what’s going to happen to your data you just entered? Maybe something like this:
How does it make you feel? Do you know what caused this error? Do you know how to avoid it next time? Do you know which button you should pound to feel safe again?
Errors in computer programs happen, and “someone” in the little box will raise a red flag and shout out. It could be the application you wrote in Excel, Excel itself, Windows running Excel, or, god forbid, the lower-level kernel of Windows (ever seen a “Blue Screen of Death?”).
Whoever raised that flag, handled the error. How? By sending you a message, by stopping the application or by crashing Windows.
The higher-up the chain the error is handled – the friendlier experience the user has.
For that reason alone, you should make sure it is YOUR application that is handling the error.
When you handle errors, you have the most control over the situation, what can be done and how to better engage the user in order to resolve the situation, and not less important, to transform a potential situation of resentment, to an opportunity for making friends.
How would you FEEL, if instead of the above message, you would receive something like that:
Exactly my point!
Every decent programming language offers error handling mechanism to the programmer. The basic approach is:
So, the basic concept is that of errors that are being RAISED and code you write to HANDLE the error. In the error handler, you can control the situation, rollback any data activities, notify the user, or do nothing and just let it pass. In the FINALIZE section you perform tasks you must do anyway before you return control to the user (or to the calling function), such as closing any open files, hiding helper Worksheets, clearing the status bar, re-enabling events, etc.
Watch the video here as I demonstrate how to implement error handling in Excel VBA – a sample lesson from my complete Computer Programming and Databases with Excel and SQL program.
Join today to the Excel VBA Inner Circle with Mor Sagmon.Get a weekly lesson, weekly live Q&A, monthly deep-dive workshop, monthly challenge and other activities.