What Happens When an Error Occurs

If VBA encounters an error and you have no error checking code in place, then the program stops and you (or your client) will be presented with the Continue, End, Debug, Help error message as shown in Figure 23.1.

When presented with the choice to End or Debug, you should click Debug. The Visual Basic Editor highlights the line that caused the error in yellow. You can hover the mouse pointer over any variable to see the current value of the variable. This provides a lot of information about what could have caused the error (see Figure 23.2).

Excel is notorious for returning errors that are not very meaningful. For example, dozens of situations can cause a 1004 error. Being able to see the offending line highlighted in yellow, plus being able to examine the current value of any variables, will help you to discover the real cause of an error.

After examining the line in error, click the Reset button to stop execution of the macro. The Reset button is the square button under the Run item in the main menu, as shown in Figure 23.3.

0 0

Post a comment