Microsoft KB Archive/111725

= FOR Behaves Differently in Visual Basic than in XLM Macro =

Article ID: 111725

Article Last Modified on 10/10/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q111725



SUMMARY
In the versions of Microsoft Visual Basic for Applications that ship with Microsoft Excel versions 5.0 and later, FOR loops behave differently than they do in the Microsoft Excel version 4.0 macro language.

The respective macro commands are as follows:

Visual Basic For counter = startValue To endValue Next counter Microsoft Excel Version 4.0 Macro Commands =FOR("counter",startValue,endValue) =NEXT



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. In Visual Basic, the number of iterations for the loop cannot be changed by changing the value of the variable used to set the ending value for the loop while the loop is in progress (this behavior is standard for most programming languages that use FOR loops). However, you can change the value of the variable in a Microsoft Excel version 4.0 style macro using the equivalent functions, FOR and NEXT.

Microsoft Excel Version 4.0 Macro Example
In the following Excel 4.0 style macro, the loop is originally set to run ten times. However, it will only run 5 times because the ending value is modified during the loop. To test this behavior, enter the following on a Microsoft Excel 4.0 macro sheet:

A1: y=10

A2: =FOR("x",1,y)

A3: y=5

A4: =NEXT

A5: =ALERT(x)

A6: =RETURN

To run the macro, select cell A1, click Macro on the Tools menu, and then click Run.

The ALERT statement in A5 will display the value 6, which means that the loop only ran 5 times, as opposed to the 10 times that the original value of "y" was set for.

Sample Visual Basic Procedure
In the following Visual Basic macro, the loop is originally set to run 10 times and it will run 10 times, even though the macro changes the value of the variable that is used to set the ending value of the loop.

To test this macro:

  Enter the following in a new Visual Basic module: Sub MyLoop Dim x, y As Integer

y = 10 For x = 1 To y             y = 5 Next x         MsgBox x      End Sub  Position the insertion point in the line that reads "Sub MyLoop" and either press F5 or click Start on the Run menu.

The MsgBox statement in the above macro displays the value 11, which means the loop ran through 10 times, even though we changed the value of the variable used to set the ending value of the loop.

