Since VBA does not have a Continue statement (or anything of that nature), what is another way of accomplishing the same thing.
Asked
Active
Viewed 224 times
-3
-
what about that old standby the goto statement? – Richard Chambers Jan 19 '15 at 20:17
2 Answers
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
-
and if there are multiple places you want to jump to the end of the loop? – Richard Chambers Jan 19 '15 at 20:20
-
-
-
-
-
Too hard to follow, ugly, blah blah blah. You'd seriously prefer multiple Goto's (and the necessary labels) to nested `if` statements? – Robert Harvey Jan 19 '15 at 20:22
-
http://www.drdobbs.com/cpp/what-dijkstra-said-was-harmful-about-got/228700940 – Richard Chambers Jan 19 '15 at 20:23
-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
-
1Arguably, 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