Excel VBA Error Function
VBA Error Function: The Error function in VBA returns the error message corresponding to a supplied error code.
Syntax
Error ([ErrorNumber])
Parameter
ErrorNumber (optional) – This parameter represents the required error number. By default, the Error function returns the most recent run-time error.
Return
This function returns the error message corresponding to a supplied error code. It returns an empty String, if the parameter ErrorNumber is zero or is skipped and there have not been any run-time errors.
Example 1
Sub ErrorFunction_Example1() ' Display the error messages for blank error code. Dim error_Msg As String error_Msg = Error() 'error_Msg will return "". MsgBox (error_Msg) End Sub
Output
Example 2
Sub ErrorFunction_Example2() ' Display the error messages for 11 error code. Dim error_Msg As String error_Msg = Error(11) 'error_Msg will return "Divison by zero". MsgBox (error_Msg) End Sub
Output
Example 3
Sub ErrorFunction_Example3() ' Display the error messages for 5 error code. Dim error_Msg As String error_Msg = Error(5) 'error_Msg will return "Invalid procedure call or argument". MsgBox (error_Msg) End Sub
Output