Error Handling Techniques

When a VBA procedure is running, errors can occur, as you undoubtedly know. These include either syntax errors (which you must correct before you can execute a procedure) or runtime errors (which occur while the procedure is running). This section deals with runtime errors.

Caution For error-handling procedures to work, the Break on All Errors setting must be turned off. In the VBE, choose Tools Options and click the General tab in the Options dialog box. If Break on All Errors is selected, VBA ignores your error-handling code. You'll usually want to use the Break on Unhandled Errors option.

Normally, a runtime error causes VBA to stop, and the user sees a dialog box that displays the error number and a description of the error. A good application doesn't make the user deal with these messages. Rather, it incorporates error-handling code to trap errors and take appropriate actions. At the very least, your error-handling code can display a more meaningful error message than the one VBA pops up.

CROSS- Appendix C lists all the VBA error codes and descriptions.

REFERENCE

0 0

Post a comment