Err

Returns or sets the ErrObject describing the current run-time error state.

Syntax:

  • Err [ () ]
  • Err = errorNumber
errorNumber
A Long error code to assign to the Err object. This is shorthand for Err.Number = errorNumber, since Number is the default property of ErrObject.

The Err object is intrinsic and global — there is no need to declare or construct one. Its properties are populated when a run-time error is raised, and reset to zero or zero-length strings when the active error handler exits via Resume, Resume Next, or any Exit statement, or when Err.Clear is called explicitly.

To generate a run-time error from your own code, use the Raise method rather than the Error statement, especially for class-module and Automation errors.

Example

This example uses the Number, Description, HelpContext, HelpFile, and Source properties of the Err object to construct an error-message dialog.

Dim Msg As String
On Error Resume Next       ' Defer error handling.
Err.Clear
Err.Raise 6                ' Generate an "Overflow" error.
If Err.Number <> 0 Then
    Msg = "Error # " & Err.Number & " was generated by " _
        & Err.Source & vbCrLf & vbCrLf & Err.Description
    MsgBox Msg, vbMsgBoxHelpButton, "Error", Err.HelpFile, Err.HelpContext
End If

See Also

License: CC-BY-4.0 Code license: MIT Attribution: VBA-Docs