Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Error Resume Next seemingly not working

Tags:

excel

vba

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:

enter image description here

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
like image 503
whytheq Avatar asked Dec 11 '22 16:12

whytheq


1 Answers

There is also a VBA setting that will cause On Error ... statements to be ignored and that dialog box to always appear. See this answer for more details on checking/changing the option: https://stackoverflow.com/a/3440789/381588

like image 52
Iridium Avatar answered Dec 27 '22 01:12

Iridium