3

Posting this here, in case it can help others. I was seeing some strange behavior where:

Sub somesub()
On Error GoTo Handler
    Dim x
    x = 1/0
    MsgBox("Done")
    Exit Sub
Handler:
    MsgBox("Error was handled")
End Sub

Was not catching the error.

There is a related question on SO already, at: Excel VBA Not Properly Breaking on Error, but it is asking about how to mitigate side effects of making one choice or another in the Error Trapping settings. This is asking why the error handling is not working at all.

2
  • 2
    possible duplicate of Excel VBA not properly breaking on error Commented Jun 24, 2015 at 15:33
  • 1
    Had to give you a kudos because I just had this same issue. The other post may be similar, but this post was far more helpful. Commented Nov 23, 2015 at 17:16

2 Answers 2

3

It turned out that in the VBA editor, under

Tools>>Options>>General>>Error Trapping

I had "Break on All Errors" selected, which overrides any instruction in the code to GoTo or Resume on the error. Changing the selection to "Break on Unhandled Errors" resolved my issue.

Source for answer: Five tips for handling errors in VBA

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

Comments

1

I have been struggling with this for months. Tools>>Options>>General>>Error Trapping was the exact solution !! Though I also structured my code to reset the error handler before every onerror goto label: statement as follows Put this above your desired onerror goto code:
onerror goto -1 '<<--- found in a different post that this resets the handler

Comments

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.