Locked lesson.
About this lesson
Running a loop a set number of times.
Exercise files
Download this lesson’s exercise file.
Loops: Looping x iterations with For x to y loops.xlsm22.9 KB
Quick reference
Loops: Looping x iterations with For x to y loops
Looping x number of times use For X loops
When to use
When you wish to loop a specific number of times, and want to avoid the dangers of runaway Do loops
Instructions
Syntax: Looping from start to end
For <variable> = <first> To <last>
‘Do Something
Next <variable>
Syntax: Looping from end to start
For <variable> = <last> To <first> Step -1
‘Do Something
Next <variable>
Hints & tips
- The <last> value should be the largest value you want to loop to
- The <first> value should be the smallest value you want to start the loop at
- Step does not need to be -1; you could step -2, -5, -10 or some other iteration
- Step does not need to be negative; you could step 2, 5, 10 or some other iteration
- Including the variable on the Next line is optional but can help make your code more readable
- you may exit a loop early by using the keywords "Exit For"
Lesson notes are only available for subscribers.