In his excellent article ?Error Trapping and Handling? http://xlvba.3.forumer.com/index.php?showtopic=17, johnske suggests that upon entry to your error handler code, the first thing you should do is ?clear? the currently active error and disable the error handler. He says that this is best done by using Resume (line) or Resume Next. Curiously, his example didn?t do exactly that:Originally Posted by johnske
[vba]Sub DummySub1()
?(some code here)
On Error GoTo MyErrorHandler
Workbooks.Open(?Myworkbook.xls?)
?(some code here)
Line5:
?(alternative code here)
Exit Sub
MyErrorHandler:
MsgBox ?Can?t find the workbook ? & ?MyWorkbook.xls?
Resume Line5
End Sub[/vba] Notice that he placed the ?Resume Line5? AFTER the ?MsgBox? statement. This is not a criticism, since I can see that his example is showing how to handle an error, then to go back into the main code and continue executing. Still, it does violate the recommendation he made, since it is certainly possible (not likely) for an error to occur within the MsgBox statement. Should that occur, it would seem that an infinite loop would begin, since the still active On Error GoTo statement (early in the code) would transfer execution to MyErrorHandler again, and so on. (Please correct me if I?m wrong about this.)
My question is, should you write the sequence something like the following:
[vba] MyErrorHandler:
Resume Contin
Contin:
MsgBox ?Can?t find the workbook ? & ?MyWorkbook.xls?
Resume Line5
End Sub[/vba] It seems klutzy to put the target label immediately following the Resume statement, yet that seems to be what is needed. However, that means that there is no longer an active error handler, so the second Resume would probably cause an error because it is being used outside of an active error handler. That suggests that (in my code above) the Resume Line5 should be replaced by GoTo Line5. Do I have a correct understanding of all this??