Basic Error Handling with the On Error GoTo Syntax

The basic error handling option is to tell VBA that in the case of an error, you want to have code branch to a specific area of the macro. In this area, you might have special code that alerts users of the problem and enables them to react.

A typical scenario is to add the error handling routine at the end of the macro. To set up an error handler, follow these steps:

1. After the last code line of the macro, insert the code line Exit Sub. This makes sure that the execution of the macro does not continue into the error handler.

2. After the Exit Sub line, add a label. A label is a name followed by a colon. For example, you might create a label called MyErrorHandler:.

0 -1

Post a comment