Results 1 to 16 of 16

Why do people use Exit Sub and Goto?

Threaded View

  1. #1
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Why do people use Exit Sub and Goto?

    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)?
    Last edited by foxguy; 07-25-2010 at 02:27 AM.
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1