

The VBA For Each loop is a scope that defines a list of statements that are to be repeated for all items specified within a certain collection/array of items. As you will find in the next section – it shouldn’t be used for iterating through objects/collections etc. To conclude – the For loop is useful when you want to execute some code using a series of numbers. Below a simple VBA For example with a defined Step loop:Ī Step value can also be negative hence providing you with the possibility to loop backward by providing a negative number: For this we need to use the Step statement. In some cases we might want to increment the counter by values larger than the default (>1). The key thing to remember is that the counter needs to be a whole number (integer). See below that is also possible to loop backwards by providing a start value higher than an end value: We can loop however both ways – from a lower to a higher number or the opposite – from a higher to a lower numbers.

Below a simple VBA For example loop where we simply want to print a series of numbers from 1 to 5: The For loop uses a counter as an argument, the counter if usually used within the loop itself. See examples of how to use this kind of loop. for Step 2 the Counter value will be incremented by 2 instead of 2 Statement indicating that the StepIncrement between increments will be defined The starting value of the Counter and the ending value of the Counter This can be only of VBA Native data types (e.g. Is a numeric variable, the counter index for the loop. How to declare a For Loop: For Counter = Start To End The For Loop is the most often used loop for situations when the number of iterationsvb is know before the loop is executed (as compared to the While and Do Until Loops). The For Loop is scope that defines a list of statements that will be executed repeatably for a certain number of times. Want to learn about Do While / Do Until loops? Read this post VBA For Loop
