-3

Since VBA does not have a Continue statement (or anything of that nature), what is another way of accomplishing the same thing.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135

2 Answers2

1

The best way to do it in languages that don't offer a Continue statement is to simply wrap the remaining code block in an if condition.

For i=1 to 10
    'some code here
    If I_want_to_finish_this_loop
        'do your thing
    End If
Next i

This avoids the use of a Goto, and your only cost is to reverse the condition.

If you have more than one place where you need to Continue, your best bet is to place a single Continue label in your code, and Goto it.

For i=1 to 10
    'some code here
    If I_dont_want_to_finish_this_loop
        Goto Continue
    End If
    'finish the loop
:Continue
Next i
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
-2

One way is using an enclosing Do...Loop. Of course, this won't work inside another Do...Loop, but we run into that in most programming languages.

For i=1 To 10
    Do 
        'Do everything in here and

        If I_Dont_Want_Finish_This_Loop Then
            Exit Do
        End If 

        'Of course, if I do want to finish it,
        'I put more stuff here, and then...


    Loop While False 'quit after one loop
Next i
Arlen Beiler
  • 15,336
  • 34
  • 92
  • 135
  • 1
    Arguably, the second `exit do` should be removed and `loop until true` or `loop while false` should be used instead. – GSerg Jan 19 '15 at 20:16