Consulting

Results 1 to 11 of 11

Thread: How to Force a Premature For-Loop Iteration

  1. #1

    How to Force a Premature For-Loop Iteration

    When writing For-loops, there are times when I want to branch to the loop-terminating ?Next? statement, thereby bypassing a lot of code within the loop. A simple example might look like this:
    [vba]For N=1 to 50
    Cnt = Cnt + 1
    If Cnt = 5 Then Goto NextN
    ? . . . (lots more code here)
    NextN:
    Next N[/vba]
    In a short loop, there?s ways of writing the statements to bypass certain parts of the code, but this gets more difficult as the loop becomes longer. As a last resort, I usually use a Goto in the manner shown above. Is there a statement that might look like ?Iterate N? that can be used for this purpose?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Just use Exit For

    [vba]

    For N = 1 To 50
    Cnt = Cnt + 1
    If Cnt = 5 Then Exit For
    ' . . . (lots more code here)
    Next N
    [/vba]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You could move "lots more code" into separate routines

    [vba]
    For n = 1 To 50
    Cnt = Cnt + 1
    If Cnt <> 5 Then DoStuff n
    Next n
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Know what you mean, Cyberdude. I try to avoid the use of GoTo whenever possible, but there isn't a built-in method that I know of to branch to the next cycle of the loop. You want to continue the loop, not exit the loop altogether.

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Use a case statement within the loop... e.g.
    [VBA]
    Option Explicit

    Sub DoStuff()
    '
    Dim N As Long, Cnt As Long
    '
    'some code here
    '
    For N = 1 To 50
    Cnt = Cnt + 1

    Select Case Cnt
    Case 1 To 4, 20 To 50
    'some stuff here
    Case 5
    'some other stuff here
    Case Else
    Exit For
    End Select

    Next N
    '
    End Sub
    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    There are ways around it, but it would be nice to have a single option that says "go to the next loop"

  7. #7
    geekgirlau (you MUST tell me what that means!) you win the prize for understanding what I'd like.

    "Exit For" does just that. I don't want to leave the loop . . . I just want it to do the next iteration without having to execute all the code that follows.

    Breaking it up into "submacros" is just too clumsy. I know I can do things like that, but a single statement ("Iterate N"??) would be simple much like the "Exit For" statement.

    In any case, I think you guys answered my question. No, there isn't a statement like I want. (sigh)
    Thanx gang for giving it some thought.

  8. #8
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    If you don't want to Exit the loop and you just want to do nothing but increase your loop index by 1 for certain values of Cnt, simply delete the Exit For in the case statement and that's exactly what'll happen.
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I clearly mis-understood your intent in my first reply, so now that I understand, this is how I would do it

    [vba]

    For n = 1 To 50
    Cnt = Cnt + 1
    If Cnt <> 5 Then
    'your stuff
    'more of it
    'and some more
    End If
    End Sub
    [/vba]

    similar to Malcolm, but avoiding sub-procedures, which I would only do there were functionally appropriate code to break-out, and if it improved the readability.

  10. #10
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    [quote=Cyberdude]geekgirlau (you MUST tell me what that means!)/quote]

    I'm afraid there's no deep, dark mystery here.
    • I sit at a computer all day (and sometimes half the night if I win the toss with my long-suffering other half), hence the "geek";
    • I'm not male (although okay, "girl" might be pushing the boundaries of credibility here given the big 4 oh looming ever closer);
    • I'm an Aussie (and when I first tried to setup the handle "geekgirl" it was already taken).

  11. #11
    Thank you, geekgirlau. I got the geek girl part (I thought), but the suffix "au" always threw me because I tend to see it as "geekgir lau", so I thought maybe it had Hawaiian overtones. Mystery explained.
    Don't sweat the "big 4 oh" ... next month I'll hit the bigger "seven 4".

Posting Permissions

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