title: For start To end Loop with end Variable Changing Mid-Loop
body: <p>Starting with test data:</p>
  <p><img src="https://i.stack.imgur.com/q8n5U.png" alt="Start" /></p>
  <p>And running the code:</p>
  <pre><code>Sub TestLoop()
  
  Dim LastRow As Long, CurRow As Long
  LastRow = Range(&quot;A&quot; &amp; Rows.Count).End(xlUp).Row
  
  For CurRow = 1 To LastRow
      Range(&quot;B&quot; &amp; CurRow).Value = &quot;Done&quot;
      LastRow = 2
  Next CurRow
  
  End Sub
  </code></pre>
  <p>I would expect the Loop to end at Row 2 because I changed the variable <code>LastRow</code>.  However the result is:</p>
  <p><img src="https://i.stack.imgur.com/UxDZj.png" alt="Done" /></p>
  <p>The <a href="http://msdn.microsoft.com/en-us/library/5z06z1kb.aspx" rel="nofollow noreferrer">MSDN Reference <strong>FOR VB.NET</strong> has this to say</a>:</p>
  <blockquote>
  <p>When a <code>For...Next</code> loop starts, Visual Basic evaluates <code>start</code>, <code>end</code>, and <code>step</code>. Visual Basic evaluates these values only at this time and then assigns <code>start</code> to <code>counter</code>. Before the statement block runs, Visual Basic compares <code>counter</code> to <code>end</code>. If <code>counter</code> is already larger than the <code>end</code> value (or smaller if <code>step</code> is negative), the <code>For loop</code> ends and control passes to the statement that follows the <code>Next</code> statement. Otherwise, the statement block runs.</p>
  <p>Each time Visual Basic encounters the <code>Next</code> statement, it increments <code>counter</code> by <code>step</code> and returns to the <code>For</code> statement. <strong>Again it compares <code>counter</code> to <code>end</code></strong>, and again it either runs the block or exits the loop, depending on the result. This process continues until <code>counter</code> passes <code>end</code> or an <code>Exit For</code> statement is encountered. <strong>[Emphasis mine]</strong></p>
  </blockquote>
  <p><strong>Given that it checks the <code>counter</code> against the <code>end</code> every iteration, shouldn't changing the variable change the <code>end</code>?</strong></p>