Incrementing Do/Loop, For/Next and For/Each

S

Steve Drenker

I've got several loops where I discover midway through the loop that I don't
want to process the remaining commands before the "Loop" or "Next" at the
end of the loop. I DO NOT want to exit the loop -- just increment the
counter and process the next loop for next incremented variable.

I know that variables such as "i" in For i = 1 to 100 can be incremented
mid-loop, but this is not advisable.

Is there a way to skip to the bottom of a loop without using a GoTo
statement? Or is that the only way?

The Exit For command is no good because I want to continue processing the
remaining elements, not end the loop.

TIA
Steve

Sdrenker <is at> pacbell <with a dot> net
 
G

Guest

Try a Do While loop

n = 1
Do While n < 100
n = n + 1
If something Then
n = NewValue
etc.
End If
Loop
 
N

Norman Jones

Hi Steve,

Possibly, one way:

Dim blOk as Boolean

For i = 1 to 100

blOk = True
If ConditionTrue Then
'Do something
Else
BlOk =false
End If

'Processing code

If blOK then
'More processing
End if

Next i
 
G

Guest

The usual aproach is to use an IF:

For i = 1 to 10
if (things are good) then
yada yada yada
Yada Yada yada
else
endif
next

This will actually skip to the end of the For, but allow it to keep iterating.
 
T

Tim Williams

just use an if...then block to wrap the code you might want to skip, and
test for the "skip" condition in that block.
 

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