How to Use For Next Loop in Excel VBA?
Last Updated :
13 Jun, 2022
If you are familiar with the programming you must have an idea of what a loop is, In computer programming, a loop is a sequence of statements that are repeated until a specific condition is satisfied.
In Excel VBA the "For Next" loop is used to go through a block of code a specific number of times.
Syntax:
For counter = start to end [step count]
[code block to execute]
statement 1
statement 2
statement 3
.
.
statement n
Next [counter]
Here we can use the counter or any other variable to run them as many times as we need.
Example:
When you are displaying numbers from 1 to 10 you may want to set the value of a variable to 1 and display it 10 times, increasing its value by 1 on each loop iteration. The same logic is used in VBA.
We specify how many times we have to run the loop, and then specify what code should our loop execute each time the loop runs.
A loop has 3 parts the first one is an initialization, the second is the condition under which the loop runs, and the last is increment or decrement.
The flow of the control in for loop:
- The For step is executed first. This step allows you to initialize any loop control variables and increment the step counter variable.
- Then the second step is, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement, just after the For Loop.
- After the body of the For loop executes, the flow of control jumps to the next statement. This statement allows you to update any loop control variables. .the value is updated as the value we write in the count.
- The condition is evaluated again it continues till the condition becomes false.
Flow Diagram
Now let us look at an example to understand how the for loop works. Follow the below steps to work along with this article:
Step 1: Press Alt + F11 to open the VBA(visual basic editor where we can write the code).

Step 2: Right-click on the workbook name and then insert a module.

Note: The interface of Excel may change depending upon your version.
STEP 3: Once a module is inserted we can write our code. Let's say we are writing code of print sum of first 10 numbers so our code will be as follows:
Sub Sumnumbers()
Dim Total as Integer
Dim Count as Integer
Total = 0 //initialised total as 0
For count = 1 to 10 //
Total = Total + count //
Next count //increments counter N = N + 1
MsgBox Total //prints total sum
End Sub // code ended
So, what's happening in the above code:
- Here in the first line, we write the subject of code this is mostly a point where our code starts sub is also important as it breaks large pieces of code in small parts.
- As we use to declare in our normal programming we declare two integers the first one is total where we will store the sum and the other is count to count n variables in a loop
- The "for next" loop starts here from count =1 and will run till the count is equal to 10.
- Then we will evaluate the total count by adding the count to the total:
Total = Total + count
- Then we increment the counter by 1 and repeat the above operations till the count is 10.
- At the end of the loop, we will show the output using a message box.
- Finally, we will end the subject.
Output:
55
Example 2: Printing product of odd +ve integers till 10
Sub ProductNumber()
Dim Product as Integer
Dim Total as Integer
Product = 1
For Count 1 to 10 Step 2 // when we use step 2 it tells compiler to increment
// count by +2 such as 1,3,5,7,..
Product = Product * count
Next Count
Msgbox Product
End Sub
Output:
12150
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read