Moving to the next iteration in a loop permaturely

  • Thread starter Thread starter Guest
  • Start date Start date
God forbid but you could use a Goto Statement.

Or you can simply encapsulate your logic inside an If statement.
 
Convert your "While" loop to "Do" loop, then you can "Exit Do" from any
place within the loop.
You can also "Exit For" from a "For" loop
 
Thanks. I was indeed trying to avoid the GoTo. Note, Alex, I am trying to move to the next iteration in the loop, not exit the loop entirely, so "Exit For" would not do it

Richard

----- Alex Ivanov wrote: ----

Convert your "While" loop to "Do" loop, then you can "Exit Do" from an
place within the loop
You can also "Exit For" from a "For" loo
 
Is there a way to move prematurely to the next iteration in a loop?

Lots of other languages allow you to do this:

Do While True
n = n + 1
Debug.Print n,
If n Mod 7 > 0 Then Next ' this line is illegal in VBA

Debug.Print

If n > 100 Then Exit Do

Loop


but not VBA. The modular (remember modular programming[1]?) way to do it is
to reverse the If statement:

Do While True
n = n + 1
Debug.Print n,
If n Mod 7 = 0 Then

Debug.Print

If n > 100 Then Exit Do

End If

Loop


[1] The essence of MP was that every section of code should have exactly
one entry point and one exit point. I was never really sure if Exit Do
complied with that or not, but it sure makes a whole number of things
easier.

Hope that helps


Tim F
 
Back
Top