Error, Error$ (functions)




Returns a String containing the text corresponding to the given error number or the most recent error.


Error$ returns a String, whereas Error returns a String variant.

The errornumber parameter is an Integer containing the number of the error message to retrieve. If this parameter is omitted, then the function returns the text corresponding to the most recent runtime error. If no runtime error has occurred, then a zero-length string is returned.

If the Error statement was used to generate a user-defined runtime error, then this function will return a zero-length string ("").


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

Erl (function); Err (function); Error Handling (topic).

More information