For Each Loop with Exceptions

  • Thread starter Thread starter ExcelMonkey
  • Start date Start date
E

ExcelMonkey

I have a For Each Loop that loops through my sheets and
performs certain actions. Is is possible to set the
routine up so that I can exclude certain sheets from the
loop. That is, if I pass the excluded sheets to a
variable or array, can this variable or array be used in
conjuction with the For Each Loop?

For Each sh in Workbook
Logic here that bypasses certain sheets

Next
 
Select sheets to skip by individual name:

If sh.name = "whatever" or sh.name = "something" Then
'do nothing
Else
your code
End if

Use a naming convention if you need to skip a lot:

If left(sh.name,4) = "skip" Then
'do nothing
Else
your code
End if

Steve
 
And I will not know what sheets I am omitting until the
macro runs. So I am assuming that I would have to pass
the sheets include/omit (boolean 1 or 0) to an Array.
Then call up the array within the for Each loop.

Click Box generated with every sheet as option
Choose sheets want to inlude
Pass Click_Event values as boolean to SheetArray
Pass SheetArray to sub

Other Sub ()
x = 0
For Each sh in Workbook
x = x + 1
If Sheet Array(X) = 1 Then
"do something"
Else
"do nothing"
Next
End Sub

What I was wondering was is their a command that allows
you to skip to the Next portion of the Loop? Or is that
just poor coding (i.e. Goto Next)

Thanks
 
If you are going to build a sheet array, why not just include the ones you
want to process - either that or go back to the source to determine which
ones to process. There is no sense doing the same or similar processing 2
or more times.

You can goto a label, but generally, you can handle that with an if
statement

for each
if condition then

end if
Next

is easier to follow than

for each
if condition then goto AA


AA:
Next
 

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

Back
Top