For…Next
- Last UpdatedOct 29, 2026
- 2 minute read
For and Next are built-in functions that implement an incrementing loop in a Math worksheet. The section of script included in the loop begins with the For() call and ends with the Next notation. The Next notation directs back to the beginning of the loop.
|
Function |
Group |
Execution |
String Exp. |
Windows |
Windows Embedded |
IoT View |
Thin Clients |
Mobile Access |
|---|---|---|---|---|---|---|---|---|
|
For |
Loop |
N/A |
No |
Supported |
Supported |
Supported |
Supported |
Not supported |
Syntax
For(numInitialValue, numFinalValue, numStep)
numInitialValue
The initial or starting value of the loop.
numFinalValue
The final or ending value of the loop.
numStep
The step or increment value.
Return value
This function returns the current value of the loop.
Notes
This type of For…Next loop can be used only in Math worksheets. It cannot be used in any Command animation, VBScript interface, or Python script interface. When you are using VBScript, you should use that language's own tools for looping. For more information, see Looping Through Code.
You must partner every For() call with a Next notation, although you may have any number of worksheet rows between them. And as shown in the example below, you must place the Next notation in the Tag Name column of the worksheet.
The loop ends when its current value exceeds the value of numFinalValue. Specifically, each time the worksheet is executed and the Next notation is encountered, the following happens:
-
The value of numStep is added to the current value of the loop;
-
The execution returns to the For() call at the beginning of the loop; and
-
The current value of the loop is compared to the value of numFinalValue. If the current value is less than or equal to numFinalValue, the loop is executed again. If the current value is greater than numFinalValue, the loop is skipped and execution resumes with the first row after the Next notation.
Examples
Example of For…Next loop in a Math worksheet
|
Tag Name |
Expression |
|---|---|
|
MyTag |
For(1, 5, 1) |
|
tagname |
expression |
|
tagname |
expression |
|
tagname |
expression |
|
Next |