Results 1 to 4 of 4

Thread: Exit For from other sub?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    ma,usa
    Posts
    485
    Hi everyone,
    I'm trying to structure a program quickly to understand it better.It's got about 11 pages worth of code running in one particular sub. I added a procedure and cut and pasted what seemed to be one process, but it's got a condition with "end for" in it. It occured to me I didn't know how to this.Any Help?
    SUEDO CODE EXAMPLE:

    Public sub One ()
    one zillion things
    For loop = 1 to Bazillion
    one trizillion things
    Next loop
    End Sub

    Public sub two()
    sub-set of one zillion things
    if this_thing then Exit For(from sub One)
    rest of subset
    End Sub

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Exit For will only exit from the innermost For loop. Your code will then continue to execute following the nearest "Next" statement.
    Code:
    Public Sub MyStuff
    'Stuff
    y = 5
    For j = 1 to 10
        Print j
        If j = y Then
            Exit For 
        End If
    Next
    'more stuff
    End Sub
    This will cause 1 thru 5 to be printed, continuing execution with "more stuff". You can't branch from a location in one sub to another sub, whether it's an "Exit" statement, "GoTo", or "GoSub". You can only go from one Sub to another by calling it.
    "It's cold gin time again ..."

    Check out my website here.

  3. #3
    Guest
    My apologies in advance for what will seem to be a smartass answer.

    I've been in the situation (Mondo spagetti code from self-proclaimed experts), and in my experience (20 years and counting) its better to rip the damn thing apart and rebuild it using modular programming techniques.

    For myself, I try very hard not to have more than 15 lines of working code in a function/subroutine. More than this, implies to me that more than one thing is happening. Comments, print statements, header statements, and dimensions don't count.

    You could easily mess around for 4 days having the same problem at different places.

    However you finish it,
    Good Luck
    DerFarm

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 1999
    Location
    ma,usa
    Posts
    485
    Thanks,
    I'm very tired and should stop working. I did this to fix it:

    Public sub One ()
    one zillion things
    For loop = 1 to Bazillion
    initialize bool to 0
    one-half trizillion things
    if bool = 1 exit For
    one-half trizillion things
    Next loop
    End Sub

    Public sub two()
    sub-set of one zillion things
    if this_thing then
    bool = 1
    Exit SUB 'returns me to For Loop
    rest of subset
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width