Problem changing For Loop Maximum inside the loop
Results 1 to 6 of 6

Thread: Problem changing For Loop Maximum inside the loop

  1. #1
    New Member
    Join Date
    Mar 2015
    Posts
    42
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Problem changing For Loop Maximum inside the loop

    Consider the following code:
    Code:
        Last = 10
        For I = 1 To Last
            Last = Last + 1
        Next I
    At the end of the code you find I = 11 and Last = 21.

    The implication of this is that once you set the Maximum for the loop, you can't change it.

    So you have to use a Do While loop explicitly testing for the changed Maximum.

  2. #2
    MrExcel MVP
    Join Date
    May 2009
    Posts
    16,392
    Post Thanks / Like
    Mentioned
    36 Post(s)
    Tagged
    8 Thread(s)

    Default Re: Problem changing For Loop Maximum inside the loop

    Not sure what your question is, but at the end of the looping Last = 20, not 21.

    Code:
    Sub test()
    Last = 10
        For i = 1 To Last
            Last = Last + 1
        Next i
        MsgBox Last & vbCrLf & i
    End Sub
    Joe

    When I was a young man I knew everything. Now that I'm older, I realize I know very little, and what I do know, I tend to forget!

  3. #3
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,116
    Post Thanks / Like
    Mentioned
    92 Post(s)
    Tagged
    33 Thread(s)

    Default Re: Problem changing For Loop Maximum inside the loop

    Quote Originally Posted by Mackeral View Post
    The implication of this is that once you set the Maximum for the loop, you can't change it.
    Correct... the loop limits are set immediately before the loop begins and cannot be changed by anything you do within the loop.
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

  4. #4
    New Member
    Join Date
    Mar 2015
    Posts
    42
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Re: Problem changing For Loop Maximum inside the loop

    Yes, this is what I discovered. From my point of view (and something I find that often happens to me when I've been working with a piece of code over a long time), I understand how it should work in a way that I hadn't considered when I first wrote it. But it certainly took a long time to actually figure out what was going on in code that I expected to work since it was in the system programming. There is nothing I have ever encountered before that had this limit hard wired and un-documented feature before.
    Last edited by Mackeral; Aug 6th, 2019 at 12:45 AM. Reason: Emphasizing a point

  5. #5
    New Member
    Join Date
    Mar 2015
    Posts
    42
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Problem changing For Loop Maximum inside the loop

    One other comment:
    Don't forget to explicitly update the control variable which was handled by the "For Loop".
    Last edited by Mackeral; Aug 6th, 2019 at 12:52 AM. Reason: Add a point

  6. #6
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    UK
    Posts
    33,737
    Post Thanks / Like
    Mentioned
    48 Post(s)
    Tagged
    6 Thread(s)

    Default Re: Problem changing For Loop Maximum inside the loop

    Quote Originally Posted by Mackeral View Post
    There is nothing I have ever encountered before that had this limit hard wired and un-documented feature before.
    It's actually clearly documented in the language spec.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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