I notice that a lot of people use this format for macros.
Sub Worksheet_Change(Target as Range)
On Error GoTo HandleError
If Target.Count <> 1 Then Exit Sub
Application.EnableEvents = False
' my Code
CleanUp:
Application.EnableEvents = True
Exit Sub
HandleError:
If Error_Handled() Then Resume
Goto CleanUp
End Sub
1) I was taught that it is good form to have only 1 exit from a sub, and it should be at the end of the sub, not somewhere in the middle. This code has 2 "Exit Sub"s from the middle, and it cannot exit at the end.
2) I was also taught that the flow should always be in 1 direction (except for loops), it shouldn't have any "GoTo"s that go backwards. This code has the "GoTo CleanUp" that goes backwards.
My macros use this format, that avoids both issues.
Sub Worksheet_Change(Target as Range)
On Error GoTo HandleError
Application.EnableEvents = False
If Target.Count = 1 Then
' my Code
End If
HandleError:
If Err.Number <> 0 Then
If Error_Handled() Then Resume
End If
CleanUp:
Application.EnableEvents = True
End Sub
Did the rules change after I learned? Does anyone have any comments about why mine is not good? Are there any web sites where this issue is discussed (especially sites that argue for the 1st method)?
Bookmarks