RE: for next loop problems

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

From: AA2e72E (AA2e72E_at_discussions.microsoft.com)
Date: 11/01/04


Date: Mon, 1 Nov 2004 00:36:01 -0800

The recommended way to jump out of a for/next loop is to use the Exit For
statement i.e instead of GoTo 178 use Exit For: this will cause execution to
resume on the Next LR line.

Don't use absolute branching (GoTo 178): this style of coding is a nightmare
to maintain. It is arguable that you should not use GoTo at all. If you must,
use GoTo Label where Label is a valid name followed by colon. For example:

If a=0 then goto MyLabel
'.... other statements

MyLabel:

"cliee" wrote:

> Hi I am a excel programming beginner, so my question may be stupid
>
> 1. how to jump out from a for next loop without error?
>
> eg
>
> For LR = 5 To 25 Step 4
> For LC = 1 To 5 Step 1
> If ActiveSheet.cells(LR,LC).Value <> True Then
> Worksheets("Label-s").cells(LR, LC).Value = "abcde"
> Worksheets("Label-s").cells(LR+1,LC).Value = "Art no. " & art & " " & des
> Worksheets("Label-s").cells(LR+2,LC).Value = con & " " & hen & " " & w
> GoTo 178
> End If
> Next LC
> Next LR
>
> I use (goto 178) to go to line 178 is this the right way?
> (line 178 is the end of this macro)
> -------------------------------------------------
>
>
> 2. I want the follow order for the for next loop
>
> 5,9,13,17,21,25
> so I use this
> For LR = 5 To 25 Step 4
>
> I want 1,3,5
> so I use
> For LC = 1 To 5 Step 1
>
> the result is that the variable fill in cells I do not wanted such as A8
> A13, and b6 .......
>
> the result what I want ( the cells which is fill in something) are like this....
>
> a5 c5 e5
> a6 c6 e6
> a7 c7 e7
>
> a9 c9 e9
> a10 c10 e10
> a11 c11 e11
>
> ............
>
>
>
> thanks for help
>



Relevant Pages

  • Re: how do people feel about exit function from loop
    ... stand out to me far better than a For Next loop for that, ... line, the line that starts the loop, I know ALL the exit conditions for the ... And now we get back to why Goto is "silly" when Exit is not. ... as I have knowledge of only a few languages. ...
    (microsoft.public.vb.general.discussion)
  • Re: Using Wildcards in a Select Case Statement ?
    ... you could, as above, use Exit Do. ... I have never *and I mean NEVER* needed a GoTo ... I have found a fair number of cases where a GoTo simplifies the code ...
    (microsoft.public.vb.general.discussion)
  • Re: FORSTY - The Fortran Styler :-)
    ... I've got a subroutine like that. ... you *CAN* get by without the goto. ... You artifically put a loop around all the executable code in the ... but just because Fortran has a loop exit construct. ...
    (comp.lang.fortran)
  • Re: FORSTY - The Fortran Styler :-)
    ... I would object much less to a GOTO if it is combined with procedures so that you can immediately see where the GOTO takes you. ... do this and then exit this function". ... Also, I am used to using languages with closures, where you can make the storage and files visible to the terminate function without making them global. ... You do this not because you actually ever intend it to loop, ...
    (comp.lang.fortran)
  • Re: Iteration in lisp
    ... state machine is just a tagbody and goto. ... But if you need to change the state-transition-matrix in the middle ... loop and either table-driven or separate-functions for defining the ... (tagbody state1 (trace-state1 aux) ...
    (comp.lang.lisp)