+ Reply to Thread
Results 1 to 9 of 9

For Next Loop Question

  1. #1
    GregR
    Guest

    For Next Loop Question

    Sometimes in using a For/Next Loop, I notice that there is just "Next"
    and sometimes "Next and (whatever). Is there a difference? TIA

    Greg


  2. #2
    aidan.heritage@virgin.net
    Guest

    Re: For Next Loop Question

    It's probably good practice to write

    for n=1 to 10
    do something
    next n

    but it's exactly the same as

    for n=10 to 10:do something:next

    Reason for being good practice is

    for n=1 to 10
    for m = 1 to 10
    for o=1 to 10
    for p = 1 to 10
    next
    next
    next
    next

    is harder to read!!! (and to debug - ESPECIALLY if the for next loops
    are nested further apart than my example!


  3. #3
    GregR
    Guest

    Re: For Next Loop Question

    Aidan, thanks

    Greg


  4. #4
    Tom Ogilvy
    Guest

    Re: For Next Loop Question

    An interesting fact I learned not too long ago:



    Sub ABC()
    For n = 1 To 10
    For m = 1 To 10
    For o = 1 To 10
    For p = 1 To 10
    Debug.Print n, m, o, p
    Next p, o, m, n

    End Sub

    --
    Regards,
    Tom Ogilvy


    "aidan.heritage@virgin.net" wrote:

    > It's probably good practice to write
    >
    > for n=1 to 10
    > do something
    > next n
    >
    > but it's exactly the same as
    >
    > for n=10 to 10:do something:next
    >
    > Reason for being good practice is
    >
    > for n=1 to 10
    > for m = 1 to 10
    > for o=1 to 10
    > for p = 1 to 10
    > next
    > next
    > next
    > next
    >
    > is harder to read!!! (and to debug - ESPECIALLY if the for next loops
    > are nested further apart than my example!
    >
    >


  5. #5
    GregR
    Guest

    Re: For Next Loop Question

    Tom, thanks. It is amazing that even you discover some new tidbits.
    Thanks for sharing.

    Greg


  6. #6
    Dave Peterson
    Guest

    Re: For Next Loop Question

    Learned by accident or from a post or somewhere else????

    Curious minds want to know <g>.



    Tom Ogilvy wrote:
    >
    > An interesting fact I learned not too long ago:
    >
    > Sub ABC()
    > For n = 1 To 10
    > For m = 1 To 10
    > For o = 1 To 10
    > For p = 1 To 10
    > Debug.Print n, m, o, p
    > Next p, o, m, n
    >
    > End Sub
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "aidan.heritage@virgin.net" wrote:
    >
    > > It's probably good practice to write
    > >
    > > for n=1 to 10
    > > do something
    > > next n
    > >
    > > but it's exactly the same as
    > >
    > > for n=10 to 10:do something:next
    > >
    > > Reason for being good practice is
    > >
    > > for n=1 to 10
    > > for m = 1 to 10
    > > for o=1 to 10
    > > for p = 1 to 10
    > > next
    > > next
    > > next
    > > next
    > >
    > > is harder to read!!! (and to debug - ESPECIALLY if the for next loops
    > > are nested further apart than my example!
    > >
    > >


    --

    Dave Peterson

  7. #7
    Tom Ogilvy
    Guest

    Re: For Next Loop Question

    From someone else's post. That's why were all really here <g>

    --
    Regards,
    Tom Ogilvy



    "Dave Peterson" <petersod@verizonXSPAM.net> wrote in message
    news:44776ECE.A9268912@verizonXSPAM.net...
    > Learned by accident or from a post or somewhere else????
    >
    > Curious minds want to know <g>.
    >
    >
    >
    > Tom Ogilvy wrote:
    > >
    > > An interesting fact I learned not too long ago:
    > >
    > > Sub ABC()
    > > For n = 1 To 10
    > > For m = 1 To 10
    > > For o = 1 To 10
    > > For p = 1 To 10
    > > Debug.Print n, m, o, p
    > > Next p, o, m, n
    > >
    > > End Sub
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > > "aidan.heritage@virgin.net" wrote:
    > >
    > > > It's probably good practice to write
    > > >
    > > > for n=1 to 10
    > > > do something
    > > > next n
    > > >
    > > > but it's exactly the same as
    > > >
    > > > for n=10 to 10:do something:next
    > > >
    > > > Reason for being good practice is
    > > >
    > > > for n=1 to 10
    > > > for m = 1 to 10
    > > > for o=1 to 10
    > > > for p = 1 to 10
    > > > next
    > > > next
    > > > next
    > > > next
    > > >
    > > > is harder to read!!! (and to debug - ESPECIALLY if the for next loops
    > > > are nested further apart than my example!
    > > >
    > > >

    >
    > --
    >
    > Dave Peterson




  8. #8
    Dave Peterson
    Guest

    Re: For Next Loop Question

    I missed that one. Thanks for sharing.

    It kind of looks like something that Tushar would like/do <vbg>.

    Tom Ogilvy wrote:
    >
    > From someone else's post. That's why were all really here <g>
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "Dave Peterson" <petersod@verizonXSPAM.net> wrote in message
    > news:44776ECE.A9268912@verizonXSPAM.net...
    > > Learned by accident or from a post or somewhere else????
    > >
    > > Curious minds want to know <g>.
    > >
    > >
    > >
    > > Tom Ogilvy wrote:
    > > >
    > > > An interesting fact I learned not too long ago:
    > > >
    > > > Sub ABC()
    > > > For n = 1 To 10
    > > > For m = 1 To 10
    > > > For o = 1 To 10
    > > > For p = 1 To 10
    > > > Debug.Print n, m, o, p
    > > > Next p, o, m, n
    > > >
    > > > End Sub
    > > >
    > > > --
    > > > Regards,
    > > > Tom Ogilvy
    > > >
    > > > "aidan.heritage@virgin.net" wrote:
    > > >
    > > > > It's probably good practice to write
    > > > >
    > > > > for n=1 to 10
    > > > > do something
    > > > > next n
    > > > >
    > > > > but it's exactly the same as
    > > > >
    > > > > for n=10 to 10:do something:next
    > > > >
    > > > > Reason for being good practice is
    > > > >
    > > > > for n=1 to 10
    > > > > for m = 1 to 10
    > > > > for o=1 to 10
    > > > > for p = 1 to 10
    > > > > next
    > > > > next
    > > > > next
    > > > > next
    > > > >
    > > > > is harder to read!!! (and to debug - ESPECIALLY if the for next loops
    > > > > are nested further apart than my example!
    > > > >
    > > > >

    > >
    > > --
    > >
    > > Dave Peterson


    --

    Dave Peterson

  9. #9
    Chip Pearson
    Guest

    Re: For Next Loop Question

    You don't need the control variable in the Next statement.

    Next
    works exactly the same way as
    Next Var

    That said, you should always use "Next Var" for documentation
    purposes. It make debugging and maintaining code much simpler
    when you can see what the Next is refering to, especially in long
    procedures.


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com



    "GregR" <gregrivet@gmail.com> wrote in message
    news:1148659385.017693.230330@g10g2000cwb.googlegroups.com...
    > Sometimes in using a For/Next Loop, I notice that there is just
    > "Next"
    > and sometimes "Next and (whatever). Is there a difference? TIA
    >
    > Greg
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1