Problem changing For Loop Maximum inside the loop

Mackeral

Board Regular
Joined
Mar 7, 2015
Messages
249
Office Version
  1. 365
Platform
  1. Windows
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.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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
 
Upvote 0
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.
 
Upvote 0
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:
Upvote 0
One other comment:
Don't forget to explicitly update the control variable which was handled by the "For Loop".
 
Last edited:
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top