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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Mon, 02 Jan 2006 19:57:51 -0800, LurfysMa <invalid@xxxxxxxxxxxxxxx>
wrote:

>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 FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
.



Relevant Pages

  • Re: Any way to force an iteration in a For Each loop?
    ... >>Is there some way to terminate one iteration in a For Each loop ... >>For Each obChar In Selection.Characters ... >statement in the C-based languages but not in VB-based languages.) ...
    (microsoft.public.word.vba.general)
  • Any way to force an iteration in a For Each loop?
    ... Is there some way to terminate one iteration in a For Each loop ... The Exit For statement will ... For Each obChar In Selection.Characters ...
    (microsoft.public.word.vba.general)
  • Re: Safe multithreading in ASP.Net
    ... Why is your thread running in this endless loop? ... > will reach the end of it's code and it will terminate. ... > the thread still continues to execute. ... > goal of this thread is to send emails. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Program analysis
    ... known if a program will ever terminate. ... an effect on the loop condition. ... would/would not terminate you might have resolved FLT without all the 'fancy ... the Halting Problem is about programs in general: ...
    (comp.programming)
  • Re: A Good Use of End
    ... I know it isn't "the done thing" for example to write code that runs in a loop when a Timer would be more suitable, but many people do it, especially when writing applications that produce animated graphic output of some sort and it is a fairly commonly accepted practice. ... Private Sub Command1_Click ... If you click Command1 to start the loop and then click Command2 while the loop is running then on most systems the program will not actually finish. ... If ytou're running it as a compiled exe it will certainly *appear to terminate*, but it'll still be hanging on in there if you look at the lost of running apps. ...
    (comp.lang.basic.visual.misc)