What does the To and Step mean in VBA?

from high number To 1 adding (-1) each iteration

Note: It’s adding because + AND - in mathematical logic evaluate to a -


If rows = 10 then

for i = 10 to 1 step -2 would mean loop back from 10 to 1 subtracting 2 from the i in each loop cycle.

adding a Debug.Print i inside the loop may give you a better clue.

Note: turn ON the Immediate Window hitting CTRL+G or View => Immediate Window from the VBE menu bar

enter image description here


An example loop increasing by 3 on each cycle.

for i = 1 to 10 step 3
    debug.print i 
next i

enter image description here


Usage

The step-back technique is mostly used when deleting rows from a spreadsheet.

To see the logic in practice see the following

Leave a Comment