Excel macro for loop

This is a tutorial for beginners to learn excel macro for loop.

Loop sequences may be achieved by using a FOR block or a WHILE.

Macro for loop using FOR

A FOR block looks like this:

For i = 1 To 100
    Range(“A” & i) = “Number” & i
Next i

Where i is a variable that changes it’s value progressively on each loop. You can use also Next i+1 to change value by 2 units.

Excel macro for loop using WHILE

A WHILE sequence looks like this:

i = 1
While 1 <= 100
Range(“A” & i) = “Number” & i
i = i + 1

First you have to initialize the value of the variable i and then, inside the While block to change the value of it however you need. Please be advised that if you forget to change the value (i=i+1), your code may loop forever and you need to stop it by pressing Ctrl+Pause (Break).

excel macro for loop

Enjoy looping in Excel!


Write a Reply or Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.