On Error Goto 0 not resetting error trapping

15,268

You need to use On Error GoTo -1 or Err.Clear to reset error trapping.

Check this answer I posted a few months ago for a more detailed explanation.

Share:
15,268
whytheq
Author by

whytheq

Current addictions: DAX / POWERSHELL Time served with: (T-)sql / MDX / VBA / SSRS Would like more time for the following: C# Python Maxim: if you build something idiot-proof, the world will build a better idiot

Updated on July 26, 2022

Comments

  • whytheq
    whytheq almost 2 years

    I was under the impression that On Error GoTo 0 reset error handling.

    So why does On error resume next not seem to be registering in the following?

    Sub GetAction()
    Dim WB As Workbook
    Set WB = ThisWorkbook
    
    On Error GoTo endbit:
    'raise an error
    Err.Raise 69
    Exit Sub
    endbit:
    On Error GoTo 0 '<<<reset error handling?
    
    On Error Resume Next
    WB.Sheets("x").Columns("D:T").AutoFit
    MsgBox "ignored error successfully and resumed next"    
    
    End Sub
    
  • whytheq
    whytheq about 11 years
    lol - this link is spot-on - really seems like my question is a duplicate - I've voted to close it with a reference to your other answer
  • AjV Jsy
    AjV Jsy almost 9 years
    Err.Clear doesn't seem to allow another error handler to be set up. I only get the required results (skipping a few lines of code if there's an error, and then doing something similar again) with On Error GoTo -1 before setting another On Error... - see example here stackoverflow.com/questions/11998836/…