How to skip to the next cycle in a For Next loop

  • Thread starter Thread starter Southern at Heart
  • Start date Start date
S

Southern at Heart

Below is a sample of what I'm trying to do. If this is possible it will save
me LOTS of nested If/Then statements which get confusing.
thanks,


For Each c In myContacts
....
If c.FileAs = strName then {skip to Next c and start over at the beginning
of the For Statement}
....
....
Next c
 
Southern said:
Below is a sample of what I'm trying to do. If this is possible it will save
me LOTS of nested If/Then statements which get confusing.
thanks,


For Each c In myContacts
...
If c.FileAs = strName then {skip to Next c and start over at the beginning
of the For Statement}
...
...
Next c


Since VBA lacks a "Continue" statement, you will have to
simulate if in some way. Enclosing the remainder of the
loop in an If block is the usual way, but it can get messy
when there are a lot of conditions.

The traditional fallback is to use GoTo statements:

For Each c In myContacts
...
If c.FileAs = strName then GotTo Continue
...
Continue:
Next c

An obscure (silly?) alternative could be (if you do not have
any other Do loops in the For loop) to use a Do - Loop:

For Each c In myContacts
Do While True
...
If c.FileAs = strName then Exit Do
...
Loop 'Continue
Next c
 
The other way to handle this, which I'm surprised nobody has mentioned yet is:

For Each c In myContacts
.....
If c.FileAs <> strName Then
....
....
End If
Next c


Rob
 
Back
Top