Moving to the next iteration in a loop permaturely

R

RichardA

Hello

Is there a way to move prematurely to the next iteration in a loop

RichardA
 
S

solex

God forbid but you could use a Goto Statement.

Or you can simply encapsulate your logic inside an If statement.
 
A

Alex Ivanov

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
 
R

RichardA

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
 
T

Tim Ferguson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top