On Error Resume Next seemingly not working
excel, vba
Solution
An example of an error when the initial error is not closed out.
Sub GetAction()
Dim WB As Workbook
Set WB = ThisWorkbook
On Error GoTo endbit:
'raise an error
Err.Raise 69
Exit Sub
endbit:
On Error Resume Next
WB.Sheets("x").Columns("D:T").AutoFit
End Sub
Problem
I have the following two lines of code: ``` On Error Resume Next myWorkbook.Sheets("x").Columns("D:T").AutoFit ``` I've stepped into the macro and executed the line `On Error Resume Next` and then on the next line `myWorkbook...` it does the following: Why doesn't the compiler resume the next line of code? `On Error` has been liberally used throughout the procedures code; I realize best practice is to use this as little as possible but it seems to fit the purpose of this macro. Reading this SO QUESTION it says that you can't have one set of error trapping within another. How can I guarantee that one set of error trapping has been "closed off" before the code moves on - does `On Error Goto 0` reset the error trapping? If it does reset then why doesn't resume work 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 On Error Resume Next WB.Sheets("x").Columns("D:T").AutoFit End Sub ```