Use the For…Next loop in a Math worksheet
- Last UpdatedOct 29, 2026
- 3 minute read
You can use the For function and the Next tag to create a For…Next loop, which repeatedly executes some part of a Math worksheet.
Note: The For…Next loop can be used only in Math worksheets. It cannot be used in any other Built-in Language interface, such as an ActiveX or .NET Control object, a Command animation, or Screen Logic (in Screen Properties).
The rows of a Math worksheet are executed sequentially from first to last. If a For function is encountered, however, it causes the execution of certain rows — specifically, the rows between the For function and its associated Next tag — to be repeated a specified number of times. When the Next tag is encountered, the execution returns to the For function at the start of the loop.
The Next tag is somewhat different from other system tags. Its name is reserved by the system, so that you cannot use the same name for one of your project tags, but its value is not defined by the system. In fact, the Next tag cannot receive any value at all. It exists only to be the end of the For…Next loop.
In the example below, a simple For…Next loop populates the elements of an array with values (for example, multiples of 10):

Using the Size property of the array ensures the loop will repeat for every element in the array, regardless of the size of the array. You could also specify a literal value for the second parameter of the For function in order to limit the number of times the loop will repeat. In either case, remember the array elements are numbered starting from position 0; for example, For(0,9,1) will cause the loop to repeat ten times for array positions 0–9.
In the next example, each element of the array is also a class with multiple members, so a second For…Next loop is nested inside the first in order to populate all of the class members with values:

The TagsDBGetClassMemberCount function gets the number of members in the class, just as the Size property gets the number of elements in the array, and this ensures the second, nested loop will repeat for every class member of every array element. Also, the second loop uses an indirect tag in order to compose the name of the class member and then set the value of that member.
Finally, in the last example, a Goto…Label structure is inserted in order to provide an escape from the For…Next loop(s), such as if the value of the most recently set class member exceeds a specified limit:

For more information about the For function and its syntax, see For…Next.