Re: Any way to force an iteration in a For Each loop?
- From: Jay Freedman <jay.freedman@xxxxxxxxxxx>
- Date: Tue, 03 Jan 2006 13:05:24 -0500
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.
.
- Follow-Ups:
- Re: Any way to force an iteration in a For Each loop?
- From: LurfysMa
- Re: Any way to force an iteration in a For Each loop?
- References:
- Any way to force an iteration in a For Each loop?
- From: LurfysMa
- Any way to force an iteration in a For Each loop?
- Prev by Date: Re: Coding a table of constants
- Next by Date: Re: Coding a table of constants
- Previous by thread: Re: Any way to force an iteration in a For Each loop?
- Next by thread: Re: Any way to force an iteration in a For Each loop?
- Index(es):
Relevant Pages
|