Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default Does VBA have a "continue" statement like C?

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.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Does VBA have a "continue" statement like C?

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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Does VBA have a "continue" statement like C?

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


wrote in message
oups.com...
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.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Does VBA have a "continue" statement like C?

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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Does VBA have a "continue" statement like C?

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

"Rick Rothstein (MVP - VB)" wrote in
message ...
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





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Does VBA have a "continue" statement like C?

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

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Does VBA have a "continue" statement like C?

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


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Does VBA have a "continue" statement like C?

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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Continue" and Exit with For Loops Andrew Hall NZ Excel Programming 4 November 23rd 06 02:44 AM
Change default in input box from "20" to "000000"? Have macro continue on to name sheet tab? StargateFanFromWork[_4_] Excel Programming 7 October 29th 06 11:46 AM
Deleted the pivot table - continue to get the "overlap" error message [email protected] Excel Discussion (Misc queries) 0 September 27th 06 04:07 PM
Automatically click "Update Links" & "Continue" paulharvey[_13_] Excel Programming 0 June 3rd 06 05:35 PM


All times are GMT +1. The time now is 02:07 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"