ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   "Continue" and Exit with For Loops (https://www.excelbanter.com/excel-programming/377966-continue-exit-loops.html)

Andrew Hall NZ

"Continue" and Exit with For Loops
 
Most of my recent scripting has been with Javascript (for Photoshop). With
For Loops I am missing the ability to use a 'continue' command which skips to
the next iteration of the for loop. I can achieve the same with IF statements
but sometimes, when one is already within a series of nested IF statements,
this becomes rather cumbersome. I suspect I can also achieve it with
functions and / or subroutines but again that looks a messy to me. I guess I
could use Go To the first line inside the loop, while resetting the counter
(though that won't work in For Each).

How do the pro's do it in VBA?

One other question while I am at it. One of the best methods for learning
Photoshop Javascript was to got to the Adobe Share Scripts site and download
scripts and look at them. Is there an equivalent for VBA you would recommend.

TIA

Andrew

Andrew Hall NZ

"Continue" and Exit with For Loops
 
I realize I forgot to add another colosely related issue - exiting out of
nested For loops. I can do this using a boolean variable in the inner loop
that then is tested in the outer loop but is there a better way (perhaps GoTo
again)

Andrew

Bob Phillips

"Continue" and Exit with For Loops
 
Nested IFs is the way I do it, with breakouts to functions.

I suppose that you could add a single iteration For ... Next loop, and Exit
For within that, but seems kludgy to me.

--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)

"Andrew Hall NZ" wrote in message
...
Most of my recent scripting has been with Javascript (for Photoshop). With
For Loops I am missing the ability to use a 'continue' command which skips

to
the next iteration of the for loop. I can achieve the same with IF

statements
but sometimes, when one is already within a series of nested IF

statements,
this becomes rather cumbersome. I suspect I can also achieve it with
functions and / or subroutines but again that looks a messy to me. I guess

I
could use Go To the first line inside the loop, while resetting the

counter
(though that won't work in For Each).

How do the pro's do it in VBA?

One other question while I am at it. One of the best methods for learning
Photoshop Javascript was to got to the Adobe Share Scripts site and

download
scripts and look at them. Is there an equivalent for VBA you would

recommend.

TIA

Andrew




Stephen Rasey[_3_]

"Continue" and Exit with For Loops
 
'This structure assumes that for each object, many work blocks can be
processed on each object.

Dim bSkip as Boolean ' logic if true, then skip rest of for loop.
For Each obj1 in objects
bSkip = False
[Work block1
bSkip = true or false]
if bSkip Then Goto ForEndA
[Work block2
bSkip = true or false]
if bSkip Then Goto ForEndA
[Work block3
bSkip = true or false]
....
ForEndA:
last steps every object need do before next object.
Next

-Stephen Rasey




Andrew Hall NZ

"Continue" and Exit with For Loops
 
Nice one Stephen, that's exactly what I was looking for and it's very general

Andrew


All times are GMT +1. The time now is 05:11 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com