Microsoft Office Tutorials and References
In Depth Information
Trapping Runtime Errors
On Error Resume Next: If an error occurs as a statement runs, that
statement is ignored, and processing continues with the next line of
code in the procedure.
On Error GoTo 0: This statement cancels any previous On Error
GoTo or On Error Resume Next statements, so VBA handles future
runtime errors rather than your own code.
You can use the Resume statement in any error-handling code to tell VBA
exactly where to resume code execution after the runtime error occurs. The
syntax for the Resume statement can take any of the following forms:
Resume: Causes VBA to re-execute the statement that caused the error.
You want to use this statement only if the error-handling code fixed the
problem that caused the error in the first place. Otherwise, executing
the same statement again causes the same error again.
Resume Next: Causes execution to resume at the first statement after
the statement that caused the error. The statement that caused the
error doesn’t execute at all.
Resume label Causes execution to resume at the specified label .
In addition to the On Error statements, VBA includes a helpful object
known as an ErrObject, which stores the error message that pops up
onscreen when an error occurs. Each of those built-in error messages has its
own number and text. The ErrObject stores that number and text, so you
can write code to identify the error and work around it.
The ErrObject has several properties. The two main ones, which are
essential to understand first, are
Err.Number: Returns either the number (integer) of the error that
occurred or 0 for no error
Err.Description: Returns the textual description of the error that
occurred as a string
The ErrObject also supports a couple of methods whose jobs can be
summed up like this:
Err.Raise(errNo): Causes the error specified by errNo to occur. This
method generally is used for testing error-handling code. (No practical
reason exists to intentionally cause an error in actual working code.)
Err.Clear(): Clears all current properties of the ErrObject. (Err.
Number returns to 0, Err.Description returns to a null string, and
so on.)
Code created by control wizards and macro conversions may already have
error-handling code written into it. Fortunately, you can easily enter such code
 
Search JabSto ::




Custom Search