Does VBA have a "continue" statement like C?

  • Thread starter Thread starter nomail1983
  • Start date Start date
N

nomail1983

I would like to write:

for each ...
if ... then continue
... rest of for loop ...
next ...

Of course, I could write:

for each ...
if not ... then
... rest of for loop ...
end if
next ...

But I prefer the style of the first form.
 
I would like to write:
for each ...
if ... then continue
... rest of for loop ...
next ...

Of course, I could write:

for each ...
if not ... then
... rest of for loop ...
end if
next ...

But I prefer the style of the first form.

VB doesn't have a Continue statement. You can imitate one like this...

For Each ....
If .... Then Goto Continue1
.... <<rest of loop>>
Continue1:
Next

Note that I used a number on the end because if you have more than one
situation where you want the Continue, your Labels that you GoTo must be
uniquely named.

Rick
 
Not sure I correctly understand what you want (Rick may have a better
understanding of "continue" in C) but when I first read your post I thought
you wanted something like this -

For each ...
If .... then
' code
Else
Exit For
End if
Next

Regards,
Peter T
 
Not sure I correctly understand what you want (Rick may have
a better understanding of "continue" in C)

The Continue statement is just a method of skipping any remaining code for
the current iteration in a loop and moving on to the next iteration for the
loop. See the "Using break and continue Within Loops" section midway down at
this link....

http://www.le.ac.uk/cc/tutorials/c/ccccif.html

Rick
 
Ah-ha! Thanks for the link, looks like a useful course. It would appear
'Exit For' is the equivalent of 'break' and the opposite of what the OP
wants.

As 'Goto' seems to be frowned on by some (not by me) maybe 'continue' could
be done purely with appropriate If's perhaps with Else's.

Regards,
Peter T
 
As 'Goto' seems to be frowned on by some (not by me) maybe 'continue'
could be done purely with appropriate If's perhaps with Else's.

Actually, I frown upon the use of GoTo also, but the OP ruled out my
preferred method of structuring the If-Then(-Else) blocks to allow a code
fall-through for those situations where others rely on Continue statement
type constructions (which is what you alluded to)... so, I was left no
choice but to post the GoTo equivalent code for it.

Rick
 
so, I was left no
choice but to post the GoTo equivalent code for it.

:-)

A bit off topic -
Actually, I frown upon the use of GoTo also

Although I said I didn't [frown on it] there are places I certainly wouldn't
use it, eg to get out of With...End With, Select Case or For-Each object
type loops On which point I avoid Exit For in a For Each loop either despite
my earlier invalid suggestion.

However, say, to avoid adding to an already large block of nested If's I
might use Goto to skip to the next section. Do you see anything
intrinsically wrong in that type of use of GoTo.

Regards,
Peter T
 
However, say, to avoid adding to an already large block of nested If's I
might use Goto to skip to the next section. Do you see anything
intrinsically wrong in that type of use of GoTo.

No, there is nothing intrinsically wrong with it at all... and many
programmers use it just the way you describe. However, there are also lots
of programmers, like me, who never use it at all. I have no trouble
constructing, or reading, a long block of nested If-Then-ElseIf blocks, so
that is what I prefer to do. To each his/her own. One note, however... if
things are getting lengthy in a nested If-Then-ElseIf block, placing the
active code in subroutines so that most levels are simply single line Call
statements helps to make things more readable.

Rick
 
Back
Top