Skip to main content

Err object Clear method

Table of contents
  1. Syntax
  2. Remarks
  3. Examples

Clears all property settings of the Err object.

Syntax

object.Clear

The object is always the Err object.

Remarks

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

Examples

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
Loop

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>