9

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
Community
  • 1
  • 1
whytheq
  • 34,466
  • 65
  • 172
  • 267
  • @Olle Sjögren There is a lot that is specific to Excel in this post, so I do not think the generic VBA tag is suitable without re-writing to make the question relevant to all other Office products. It is unfair to force a duplicate on a person with a similar problem made specific to say, Powerpoint. – Fionnuala Mar 21 '13 at 11:53
  • BTW the more generic question is at http://stackoverflow.com/questions/14158901/difference-between-on-error-goto-0-and-on-error-goto-1-vba/14159999#14159999, as cited below. – Fionnuala Mar 21 '13 at 11:56
  • @Remou OK, no problem. I just felt that the `On Error GoTo -1` answer was general enough to warrant the [tag:VBA] tag. – Olle Sjögren Mar 21 '13 at 12:08
  • @OlleSjögren I see where you are coming from, and it is a toss-up alright, it just seems to me that this particular question has a lot of Excel in it. If you feel very strongly, I will not fight back :) – Fionnuala Mar 21 '13 at 12:10

1 Answers1

7

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.

Community
  • 1
  • 1
Francis Dean
  • 2,386
  • 2
  • 22
  • 29
  • 1
    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 – whytheq Mar 26 '13 at 08:54
  • `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 http://stackoverflow.com/questions/11998836/excel-vba-on-error-goto-statement-not-working-inside-for-loop/31543829#31543829 – AjV Jsy Jul 21 '15 at 16:11