0

I wanted to write the error handling for vba, which after directing to the error handling will first check for a specific error and if there is no match, it will show the error message as according to Excel.

Here is my code:

On Error GoTo ErrorHandl

ErrorHandl:
If Err.Number = 13 Then
  MsgBox "Do that …", vbCritical
  Exit Sub
Else
??????

What could I write in the place of questions marks, so that if error number is different than 13 then, excel will show me the other error that is currently occurring?

1
  • 1
    You could use a Message Box to show Err.Description. Commented Jun 12, 2019 at 12:42

2 Answers 2

1

This is a possible way to get custom errors with Select Case:. If the error is number 11 (Division by 0), then it gives a customized MsgBox. Othewise it gives a standard one with the Err.Number and Err.Description, based on the current language of the system (Mine is German):

enter image description here

The code:

Sub TestMe()

    On Error GoTo TestMe_Error

    Debug.Print 5 / 0

    On Error GoTo 0
    Exit Sub

TestMe_Error:

    Select Case Err.Number
    Case 11:
        MsgBox "Division by null is not allowed on this planet!"
    Case Else:
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure TestMe."
    End Select

End Sub

And if you need a multipurpose Error-Handler, this is the code:

Sub TestMe()

    On Error GoTo TestMe_Error

    Debug.Print 5 / 0

    On Error GoTo 0
    Exit Sub

TestMe_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure TestMe."

End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

I am getting error 0. I am using if loop and else ifs instead of 'Case is'. Then I get the MsgBox as you typed and gives Error(0).
@Dozens - error number 0 means no error. Did you copy+paste my solution or you wrote something else? What exactly?
The idea of the error handler is to write all your code in the place Debug.Print 5 / 0 of the code above.
the thing was that there was missing 'Exit sub' before Erroe handler
Yes, Now I see. But I have not seen it at the time.
|
0

The code you wrote now is error number specific. You could also write a general error handler

Sub test()
On error GoTo Errorhandler

 '''
 'Code
 '''

Exit Sub
Errorhandler:
MSgbox "Warning: fatal error occured." & vbCr & vbCr "Error message:" & vbCr & Err.Description, vbcritical, "Error"
End Sub

This will, regardless of which error occured, display a message box warning the user of a fatal error followed by the error message as displayed in VBE.

Of course, you could also specify the error code instead of making it general

2 Comments

The Exit sub before Errorhandler was the bull's eye for me. Thanks
Glad to have helped!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.