Error function

Category: Program Flow / Interaction

Summary

Returns or generates a runtime error. Can also be used to supply a custom error message corresponding to an error number.

Syntax

Error [ (errornumber) ] The optional errornumber argument can be any valid error number. If errornumber is a valid error number, but is not defined, Error returns the string "Application-defined or object-defined error."
If errornumber is not valid, an error occurs. If errornumber is omitted, the message corresponding to the most recent run-time error is returned. If no run-time error has occurred, or errornumber is 0, Error returns a zero-length string ("").

Example

Example
This example uses the Error function to print error messages that correspond to the specified error numbers.
Private Sub PrintError()
Dim ErrorNumber As Long, count As Long
count = 1: ErrorNumber = 1
On Error GoTo EOSb
Do While count < 100
Do While Error(ErrorNumber) = "Application-defined or object-defined error": ErrorNumber = ErrorNumber + 1: Loop
Debug.Print count & "-Error(" & ErrorNumber & "): " & Error(ErrorNumber)
ErrorNumber = ErrorNumber + 1
count = count + 1
Loop
EOSb: Debug.Print ErrorNumber
End Sub

Microsoft Support Page

https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/error-function

Back to Functions