If you are writing a VBA program & you want to execute the same task multiple times then you can do this by using VBA for loops. VBA is a sequential programming language. Each line of code gets executed from top to bottom until there are no more lines of code to read. If you want to go back & perform a task, then you have to force by using macro code. You get results with loop.
The For Loop in VBA is one of the most common types of loop. The For loop has two forms: For Next and For Each In Next. The For loop is typically used to move sequentially through a list of items or numbers. To end the For loop at any given point, we can use the exit statement. For Loop will go round and round until it meets the end condition. Once the end condition is met, the programming flow will continue downward, in its natural direction.
The For … Next loop has the following syntax: For counter = start_counter To end_counter 'Do something here (your code) Next counter
We are actually creating a loop that uses variable counter as the ‘time keeper’ of the loop. We set it to a value equal to start_counter at the beginning of the loop and then increment it by 1 during each loop till it meets the end condition. The loop will execute till the time the value of the counter becomes equal to end_counter. The loop executes for the last time when both the above values match and then the loop stops.
All of the above might be confusing for some people, so let’s gear up & start learning For Loop in excel with few examples.
Loop 1 (Displaying number with msgbox)
Sub Loop1() Dim StartNumber As Integer Dim EndNumber As Integer EndNumber = 5 For StartNumber = 1 To EndNumber MsgBox StartNumber & " is " & "Your StartNumber" Next StartNumber End Sub
Code Explanation:
Loop2 (Fill Values)
Sub Loop2() 'Fills cells A1:A56 with values of X by looping' --- Comment 'Increase value of X by 1 in each loop' --- Comment Dim X As Integer For X = 1 To 56 Range("A" & X).Value = X Next X End Sub
Code Explanation:
Loop3 (Fill cells with background color)
Sub Loop3() ' Fills cells B1:B56 with the 56 background colours' --- Comment Dim X As Integer For X = 1 To 56 Range("B" & X).Select With Selection.Interior .ColorIndex = X .Pattern = xlSolid End With Next X End Sub
Code Explanation:
Loop 4 (Fill Values with increment of 2)
By default, the Step value is forward 1, however it can be set to a number more than 1.
Sub Loop4() ' Fills every second cell from C1:C50 with values of X' --- Comment Dim X As Integer For X = 1 To 50 Step 2 Range("C" & X).Value = X Next X End Sub
Code Explanation:
Loop 5 (VBA For Loop in Reverse with STEP Instruction)
It is not necessary that counter in the For loop will only move from low to higher values; instead, For loop can run backwards, too i.e. high to lower values.
Even though the Step value is forward 1 by default, however, it can be set to a number in reverse order.
Sub Loop5() ' Fills cells from D1:D50 with values of X' --- Comment ' In this case X decreases by 1' --- Comment Dim X As Integer, Row As Integer Row = 1 For X = 50 To 0 Step -1 Range("D" & Row).Value = X Row = Row + 1 Next X End Sub
Code Explanation:
Loop 6 (Fills every second cell in Reverse with STEP-2)
In the above For loop example, we can use the Step and order to see if the For loop works in forward or backward direction.
Sub Loop6() ' Fills every second cell from E1:E100 with values of X' --- Comment ' In this case X decreases by 2' --- Comment Dim X As Integer, Row As Integer Row = 1 For X = 100 To 0 Step -2 Range("E" & Row).Value = X Row = Row + 2 Next X End Sub
Code Explanation:
Loop 7 (For Loop with IF condition: Fills cells starting from specific cell)
This will fill the cells from cell F11 with value 11 till X meets the IF condition
Sub Loop7() ' Starts to fill cells F11:F100 with values of X' --- Comment ' This will exit from the loop after 50' --- Comment Dim X As Integer For X = 11 To 100 Range("F" & X).Value = X If X = 50 Then MsgBox ("Bye Bye") Exit For End If Next X End Sub
Code Explanation:
Conclusion: With the above 7 examples, we can apply For loop in our regular or any automation part.
If you liked our blogs, share it with your friends on Facebook. And also you can follow us on Twitter and Facebook.
We would love to hear from you, do let us know how we can improve, complement or innovate our work and make it better for you. Write us at info@exceltip.com
The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.
??????????? ??? ???????!
?????????? ?????? ???????? ??????? ?? ?????? ??? ???? ? ?????.???? ??????????? ??? «??????????» ???????? 10 ??? ?? ????? ???? ????????? ? ????????.
?? ????? ?????????? ??? ???????????? ?????????? ?????,??????? ?????????? ???????????,??????? ?????? ?? ??????????? ??????,?????????? ????? ? ??? ? ?????????? ?????????? ?? ??????,? ??????????????? ???????? ? ??????????? ???????????
????? ????????? ?????????? ????????? ?? ????? ?????
http://www.space-prod.com/index/8-9728
http://lastmoon.org/forum/index.php?action=profile;u=67470
https://hulafrog2.com/burlington-winchester-ma/register-business/?er=captcha&email=b.l.i.zko.y.a.n2.017%40gmail.com&bname=Bogdanxnt&fn=Bogdanxnt&busi=Bogdanxnt&web=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D1PoE0qZwcokJRhjHqUDuNciFhoeYgqTh0&fb=https%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D1PoE0qZwcokJRhjHqUDuNciFhoeYgqTh0&phone=84463529756&add1=Minsk&add2=Minsk&town=Minsk&state=MN&zip=151414&cat=&subcat=&about=%D0%94%D0%BE%D0%B1%D1%80%D1%8B%D0%B9%20%D0%B4%D0%B5%D0%BD%D1%8C%20%D0%B4%D0%B0%D0%BC%D1%8B%20%D0%B8%20%D0%B3%D0%BE%D1%81%D0%BF%D0%BE%D0%B4%D0%B0%3Ca%20href%3Dhttps%3A%2F%2Fstekloelit.by%3E%21%3C%2Fa%3E%20%0D%0A%D0%9F%D1%80%D0%B5%D0%B4%D0%BB%D0%B0%D0%B3%D0%B0%D0%B5%D0%BC%20%D0%92%D0%B0%D1%88%D0%B5%D0%BC%D1%83%20%D0%B2%D0%BD%D0%B8%D0%BC%D0%B0%D0%BD%D0%B8%D1%8E%20%D0%B8%D0%B7%D0%B4%D0%B5%D0%BB%D0%B8%D1%8F%20%D0%B8%D0%B7%20%D1%81%D1%82%D0%B5%D0%BA%D0%BB%D0%B0%20%D0%B4%D0%BB%D1%8F%20%D0%B4%D0%BE%D0%BC%D0%B0%20%D0%B8%20%D0%BE%D1%84%D0%B8%D1%81%D0%B0.%D0%9D%D0%B0%D1%88%D0%B0%20%D0%BE%D1%80%D0%B3%D0%B0%D0%BD%D0%B8%D0%B7%D0%B0%D1%86%D0%B8%D1%8F%20%D0%9E%D0%9E%D0%9E%20%C2%AB%D0%A1%D0%A2%D0%95%D0%9A%D0%9B%D0%9E%D0%AD%D0%9B%D0%98%D0%A2%C2%BB%20%D1%80%D0%B0%D0%B1%D0%BE%D1%82%D0%B0%D0%B5%D1%82%2010%20%D0%BB%D0%B5%D1%82%20%D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0%91%D0%B5%D0%BB%D0%B0%D1%80%D1%83%D1%81%D0%B8.%20%0D%0A%D0%9C%D1%8B%20%D0%BC%D0%BE%D0%B6%D0%B5%D0%BC%20%D0%BF%D1%80%D0%B5%D0%B4%D0%BB%D0%BE%D0%B6%D0%B8%D1%82%D1%8C%20%D0%92%D0%B0%D0%BC%20%3Ca%20href%3Dhttps%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D1PoE0qZwcokJRhjHqUDuNciFhoeYgqTh0%3E%D0%9C%D0%B5%D0%B6%D0%BA%D0%BE%D0%BC%D0%BD%D0%B0%D1%82%D0%BD%D1%8B%D0%B5%20%D1%81%D1%82%D0%B5%D0%BA%D0%BB%D1%8F%D0%BD%D0%BD%D1%8B%D0%B5%20%D0%B4%D0%B2%D0%B5%D1%80%D0%B8%3C%2Fa%3E%2C%3Ca%20href%3Dhttps%3A%2F%2Fdrive.google.com%2Fopen%3Fid%3D1PoE0qZwcokJRhjHqUDuNciFhoeYgqTh0%3E%D0%9E%D1%84%D0%B8%D1%81%D0%BD%D1%8B%D0%B5%20%D1%81%D1%82%D0%B5%D0%BA%D0%BB%D1%8F%D0%BD%D0%BD%
http://www.akbard.kz/festival/user/Bogdanlbs/
http://banglagamer.com/member.php?396757-Mihaeluhp
Please , i have this problem, my code
for i = 1 to 100
if worksheets("sheet1").cells(i,1).value = i ------------this dont work
when i change i to 1 it works like this
if worksheets("sheet1").cells(i,1).value = 1-----------work
i try convert the cell covert the every value no way , thanks
Hi Baseam,
let me know if it works for you.
I am excited to see wonderful blog for VBA in simple terms great codes
Thanks, V S GAUTHAMI... There's a wave of these simple VBA explanations coming. But if you want any specific term to understand, you can mention it here.
Nice
Nice blog with simple example. Very Nice...
Hello,
Very good blog with simple & smart examples on how to use For Loops. I have bookmarked this link for future references.
Very much appreciated.
Mitchel
Thanks Mitchel...