Any way to force an iteration in a For Each loop?

L

LurfysMa

Is there some way to terminate one iteration in a For Each loop
without terminating the loop itself? The Exit For statement will
terminate the loop. I thought a Next statement might do it, but I cant
get that to work.

Here's what I tried:

For Each obChar In Selection.Characters
Call MsgBox(obChar.Text)
If obChar.Text = "A" Then Next obChar
Call MsgBox("Not an 'A'")
Next obChar

This gets an error on line 3 saying there is a Next without a For.
 
G

Greg Maxey

Not sure what it is that you want to do.

Sub Test()
Dim obChar As Range
For Each obChar In Selection.Characters
MsgBox obChar.Text
If Asc(obChar) <> 65 Then
MsgBox ("I'm not an 'A'")
End If
Next obChar
End Sub
 
L

LurfysMa

Not sure what it is that you want to do.

The example was simplified to make it easier to read. The actual
application has a lot more code. There are several classes of elements
in the range: vowels, consonants, punctuation, etc. They are
hierarchical. These is some processing that will be done on all of
them. Then some that will be done on all but the vowels. Then some
more that will be done on all but the vowels and the consonants. And
so on.

I want to loop through all elements ending the specific iteration, but
not the entire loop, after the necessary work has been done.

Sub Test()

Dim obChar As Range

For Each obChar In Selection.Characters
...several lines of code that is done on all characters...
If <some test for vowels> Then Next For
...several more lines of code for the remaining characters...
If <some test for consonents> Then Next For
...several more lines of code for the remaining characters...
If <some test for something else> Then Next For
...several more lines of code for the remaining characters...
Next obChar

End Sub

My question remains: is there a way to end an iteration without ending
the loop?
 
D

Doug Robbins - Word MVP

I think that to do what you mean you should be using If ...Then....Else
If...Then ....End If

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
L

LurfysMa

I think that to do what you mean you should be using If ...Then....Else
If...Then ....End If

Yes, I know several ways to code it up. But they are all messy. They
involve many nested levels of Ifs or other constructs.

Can anyone just answer my question without telling me some other way
to do it?

Is there any way to terminate a single iteration of a loop without
terminating the whole loop? If not, just say so and I'll find another
way.

I once used another language (Rexx) that had this feature and it was
very powerful. It worked like this:

Do i=1 to n
...some code...
If condition1 then Iterate i
...some more code
If condition2 then Iterate i
...some more code...
If condition3 then Iterate i
...etc...
End i

The Iterate statements acted as if the End statement had been
encountered. Is was like an immediate jump to the End statement. The
loop conditions would then be evaluated and the loop would either exit
or continue with the next iteration.

If VB or VBA can't do it, it can't.

So, can it or can't it?
 
D

Doug Robbins - Word MVP

The only way for other people to really understand what you are trying to
simplify will probably be for you to post all of the code.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
L

LurfysMa

The only way for other people to really understand what you are trying to
simplify will probably be for you to post all of the code.

Why? I just want to know if there is any way to force an iteration on
a For Each loop. No one has said "yes" or "no".

Apparently, the answer is "no". Not sure why that is so hard to admit.
 
D

Doug Robbins - Word MVP

I won't say "No" because I don't know that it is the correct answer. The
one thing that I do know is that with VBA, "there are many ways to skin the
cat."

If I knew exactly what you were trying to do, I might be able to suggest a
way of doing what you want, which may or may not be exactly the same as
answering that specific question.

Your simplified examples do not allow me to do that.

Now I suppose I could sit here and try and dream up something that is
equivalent to what you are trying to do, but I am not going to do that.

If you would like an accurate answer to the question, post the code.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Helmut Weber

Hi,
I just want to know if there is any way to force an iteration on
a For Each loop. No one has said "yes" or "no".
Yes, I know several ways to code it up. But they are all messy.

What ways do you have in mind?

Goto?
Introducing a boolean variable
which would prevent execution of following commands?

Or:

If not condition1 then
...some more code
If not condition2
...some more code...
If not condition3
...etc...

Googling for "next iteration" though,
brings up lot of workaraounds,
but nothing like "iterate this loop".


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Jay Freedman

Is there some way to terminate one iteration in a For Each loop
without terminating the loop itself? The Exit For statement will
terminate the loop. I thought a Next statement might do it, but I cant
get that to work.

Here's what I tried:

For Each obChar In Selection.Characters
Call MsgBox(obChar.Text)
If obChar.Text = "A" Then Next obChar
Call MsgBox("Not an 'A'")
Next obChar

This gets an error on line 3 saying there is a Next without a For.

There is no "direct" way to do this in VBA. (There's a "continue"
statement in the C-based languages but not in VB-based languages.)

The "indirect" way is to use GoTo to jump to a label just before the
Next statement:

For Each obChar In Selection.Characters
Call MsgBox(obChar.Text)
If obChar.Text = "A" Then GoTo SkipIt
Call MsgBox("Not an 'A'")
SkipIt:
Next obChar

This is OK for very occasional use. The problem is that if you
over-use it, you get classic spaghetti code. You're better off,
overall, using whatever depth of nested If...Then...Else structures is
necessary.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
L

LurfysMa

There is no "direct" way to do this in VBA. (There's a "continue"
statement in the C-based languages but not in VB-based languages.)

Thank you. That's what I concluded because I couldn't find any such
construct, but I have missed things in the help before so I thought
I'd ask those more knowledgeable than I.
The "indirect" way is to use GoTo to jump to a label just before the
Next statement:

For Each obChar In Selection.Characters
Call MsgBox(obChar.Text)
If obChar.Text = "A" Then GoTo SkipIt
Call MsgBox("Not an 'A'")
SkipIt:
Next obChar

This is OK for very occasional use. The problem is that if you
over-use it, you get classic spaghetti code. You're better off,
overall, using whatever depth of nested If...Then...Else structures is
necessary.

Thanks. Good workaround -- for occasional use. ;-)
 

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

Top