Error (statement)


Error errornumber


Simulates the occurrence of the given runtime error.


The errornumber parameter is any Integer containing either a built-in error number or a user-defined error number. The Err function can be used within the error trap handler to determine the value of the error.


This example forces error 10, with a subsequent transfer to the TestError label. TestError tests the error and, if not error 55, resets Err to 999 (user-defined error) and returns to the Main subroutine.

Sub Main()
  On Error Goto TestError
  Error 10
  MsgBox "The returned error is: '" & Err() & " - " & Error$ & "'"
  Exit Sub

  If Err = 55 Then    'File already open.
    MsgBox "Cannot copy an open file. Close it and try again."
    MsgBox "Error '" & Err & "' has occurred."
    Err = 999
  End If
  Resume Next
End Sub

See Also

Err (statement); Error Handling (topic).

More information