This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Download Microsoft Edge
More info about Internet Explorer and Microsoft Edge
The
object
is always the
Err
object.
Use
Clear
to explicitly clear the
Err
object after an error has been handled, for example, when you use deferred error handling with
On Error Resume Next
. The
Clear
method is called automatically whenever any of the following
statements
is executed:
Any type of
Resume
statement
Exit Sub
,
Exit Function
,
Exit Property
Any
On Error
statement
The
On Error Resume Next
construct may be preferable to
On Error GoTo
when handling errors generated during access to other objects. Checking
Err
after each interaction with an object removes ambiguity about which object was accessed by the code. You can be sure which object placed the error code in
Err.Number
, as well as which object originally generated the error (the object specified in
Err.Source
).
Example
This example uses the
Err
object's
Clear
method to reset the numeric properties of the
Err
object to zero and its string properties to zero-length strings. If
Clear
were omitted from the following code, the error message dialog box would be displayed on every iteration of the loop (after an error occurs) whether or not a successive calculation generated an error. You can single-step through the code to see the effect.
Dim Result(10) As Integer ' Declare array whose elements
' will overflow easily.
Dim indx
On Error Resume Next ' Defer error trapping.
Do Until indx = 10
' Generate an occasional error or store result if no error.
Result(indx) = Rnd * indx * 20000
If Err.Number <> 0 Then
MsgBox Err, , "Error Generated: ", Err.HelpFile, Err.HelpContext
Err.Clear ' Clear Err object properties.
End If
indx = indx + 1
See also
Statements (Visual Basic for Applications)
Objects (Visual Basic for Applications)
Support and feedback
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.