The For Loop is perhaps the most useful of all the Loops. It runs a line or lines of code a set amount of times in any increment we set. The default increment is one. As you can see from below you must use a Variable of the Numeric type to set the amount of Loops it will perform.
Sub My_For()
Dim iMyNumber as Integer
For iMyNumber= 1 To 100
iMyNumber=1+iMyNumber
Range(“b2”).Value = iMyNumber
Next iMyNumber
End Sub
The other great part about the For Loop is we can increment by any Value we like. We do this by using the Step Key word and telling it the Step (or increment) to use. so we could use:
Sub My_For1()
Dim iMyNumber as Integer
Dim iMyNumber2 as Integer
For iMyNumber= 1 To 100 Step 2
iMyNumber2 =1+iMyNumber2
Next iMyNumber
Range(“b2”).Value = iMyNumber
End Sub
By doing this we will Loop through our code 51 times instead of 101 times, but the Variable iMyNumber will end up with a Value of 101.
We could also use the Step Key word to work backwards like below:
Sub My_For2()
Dim iMyNumber as Integer
Dim iMyNumber2 as Integer
For iMyNumber= 1000 To 1 Step -1
iMyNumber2 =1+iMyNumber2
Next iMyNumber
Range(“b2”).Value = iMyNumber
End Sub
This would mean that our Variable iMyNumber would end up with a Value of 0 (Zero).