How to use For Loop in Excel VBA easily (4 Examples)

Welcome to Excel Avon

For Loop in Excel VBA

DOWNLOAD USED EXCEL FILE FROM HERE>>

For Loop in excel VBA are considered one such component of VBA that can take hours to a human. It can produce results in minutes. Looping is one of the hottest programming techniques. A loop in Excel VBA enables you to loop through a range of cells with just a few code lines. A loop enables you to loop through a string of cells with just a few code lines in Excel VBA. To repeat the process with one variable is used while loop in excel VBA.

To understand For Loop in Excel VBA, we have to go to VBE – Like last time, first go to the Developer Tab, then we will click on the Visual Basic option as shown in the image below.

For-loop- in excel-VBA

On opening in VBE, you have to go to Insert and then Model has to be inserted, as can be seen in the image.

for-loop-in-Excel-VBA

Only For Loop in Excel Avon

Now I will write Subroutine for only Loop in excel vba.

Sub UseForLoop()

End Sub

After writing subroutine, I will Define variable.

Sub UseForLoop()
Dim i as Integer

End Sub

Now we will define the loop We will use this loop to square all numbers from 1 to 100.

Sub UseForLoop()
Dim i as Integer

for i = 1 to 100
Next i 
End Sub

and debug.Print and will print the square of all the numbers from 1 to 100 in the Immediate window.

Sub UseForLoop()
Dim i as Integer

for i = 1 to 100
debug.print i ^ 2
Next i 
End Sub

Then click on Run button and see in Immediate window

for-loop-in-excel-VBA

Now you can see that the square of all the numbers from 1 to 100 will be printed in the Immediate window.

for-loop-in-excel-VBA.png1

For Loop with step in Excel VBA

Now we will write the subroutine of loop with step

Sub UseForWithStep()

End Sub

After writing subroutine, I will Define variable.

Sub UseForWithStep()
Dim i as Integer

End Sub

Now we will define the loop we will use this loop to square all numbers from 1 to 100.

Sub UseForLoop()
Dim i as Integer

for i = 1 to 100
Next i 
End Sub

and debug.Print Select and print the square of all numbers from 1 to 100 in the Immediate Window. But a condition will also apply in this, the condition will be something like this, we will do the square by skipping 2 steps in the square from 1 to 100

Sub UseForLoop()
Dim i as Integer

for i = 1 to 100 step 2
debug.print i ^ 2
Next i 
End Sub

Then click on Run button and see in Immediate window

for-loop-in-excel-VBA

Now you can see that the square of all the numbers from 1 to 100 will be printed in the Immediate window. Now you can see that the square of all the numbers from 1 to 100 will be printed in the immediate window. But by skipping 2 steps, something like this the square would be 1, 3, 5, 7, 9, ………….,100.

for-loop-in-excel-VBA.png2

Negative Step For Loop in Excel

We will only add condition e.g. square of all reverse integers from 100 to 1

Now we will write the subroutine of loop with step

Sub UseForWithStep()

End Sub

After writing subroutine, I will Define variable.

Sub UseForWithStep()
Dim i as Integer

End Sub

Now we will define the loop we will use this loop to square all numbers from 100 to 1. But will use -1 because every step will be followed by a step down.

Sub UseForLoop()
Dim i as Integer

for i = 100 to 1 step -1
Next i 
End Sub

Now we will use debug.print

Sub UseForLoop() 
Dim i as Integer 

for i = 100 to 1 step -1
      debug.print i ^ 2 
Next i 
End Sub

Then click on Run button and see in Immediate window

for-loop-in-excel-VBA

In reverse, the square of all Integer from 100 to 1 will be printed in the Immediate window.

for-loop-in-excel-VBA.png3

For Loop With Exit in Excel VBA 

Now I will write subroutine loop with Exit.

Sub UseForWithExit()

End Sub

After writing subroutine, I will Define variable.

Sub UseForWithExit()
Dim i as Integer

End Sub

Now we will define the for loop we will use this loop to cube all numbers from 1 to 100.

Sub UseForWithExit()
Dim i as Integer

for i = 1 to 100
Next i 
End Sub

Along with cubing, we will also apply a condition like cube all numbers up to 20.

Sub UseForWithExit()
Dim i as Integer

for i = 1 to 100 step 2
debug.print i ^ 3
If i = 20 Then 
   Exit for
End If 
Next i 
End Sub

Then click on Run button and see in Immediate window

for-loop-in-excel-VBA

Now you will see that all the numbers from 1 to 20 will be printed as cube result in the Immediate window.

for-loop-in-excel-VBA.png4

So, I hope you have understood How to use For Loop in Excel VBA and for more information, you can follow us on Twitter, Instagram, LinkedIn, and YouTube as well.

You can also see well-explained video here about Logical Operator 

Leave a Reply