Microsoft Office Tutorials and References
In Depth Information
Error-Handling Techniques
To cause your VBA code to continue when an error occurs, insert the following statement in your
On Error Resume Next
Some errors are inconsequential, and you can ignore them without causing a problem. But you
might want to determine what the error was. When an error occurs, you can use the Err object to
determine the error number. You can use the VBA Error function to display the text that
corresponds to the Err.Number value. For example, the following statement displays the same
information as the normal Visual Basic error dialog box (the error number and the error description):
MsgBox “Error “ & Err & “: “ & Error(Err.Number)
Figure 9-6 shows a VBA error message, and Figure 9-7 shows the same error displayed in a
message box. You can, of course, make the error message a bit more meaningful to your end users
by using more descriptive text.
Referencing Err is equivalent to accessing the Number property of the Err object.
Therefore, the following two statements have the same effect:
MsgBox Err
MsgBox Err.Number
You also use the On Error statement to specify a location in your procedure to jump to when
an error occurs. You use a label to mark the location. For example:
On Error GoTo ErrorHandler
Search JabSto ::

Custom Search