Does your code need to execute the same action more than once? Instead of programming it multiple times, try writing it once and repeating it with a loop.
VBA, or Visual Basic for Applications, is Excel's built-in programming language. It lets you write macros that automate repetitive tasks across your spreadsheets, from formatting cells to processing thousands of rows of data. A VBA For loop is one of the most fundamental control structures in VBA. It repeats a block of code a specified number of times, using a counter variable that increments with each pass.
Think of it this way: instead of manually editing 500 cells one at a time, a for loop VBA macro handles the entire operation in seconds. Whether you need to populate a column with calculated values, apply formatting across a range or cycle through a list of records, a VBA loop gives your code the ability to do the heavy lifting.
VBA offers several loop types beyond the For loop, and each one is suited to different situations. The next section compares them so you can choose the right structure for your task.
Excel offers several different loop structures, depending on the conditions under which it should execute. Check out the table below to see how each loop works and an example of what it might look like as driving directions.
| VBA Loop | How It Works | Real-Life Analogy | Best Used When |
|---|---|---|---|
| For...Next | Executes a certain number of times | "Turn right three times, then stop." | You know exactly how many times the code should repeat |
| Do...Until | Executes until a condition is true, then stops | "Keep going until you cross the railroad tracks, then turn left." | You want the loop to stop when a specific condition is met |
| Do...While | Executes while a condition is true and stops when it is no longer true | "Keep going while the ditch runs alongside the road, then turn right." | You want the loop to continue only as long as a condition remains true |
| For Each | Iterates through every item in a collection | "Stop at every house on the street and deliver a package." | You need to process every object in a group such as each cell in a range or each worksheet in a workbook |
Choosing the right loop type comes down to one question: do you know in advance how many times the code needs to run? If yes, a For...Next loop is usually the best choice. If no, a Do...While or Do...Until loop lets the data itself determine when to stop. And when you need to act on every item in a collection without tracking a counter, a For Each loop keeps your code clean and readable.
Read on for a walkthrough of each loop type with code examples you can adapt for your own Excel macros.
To repeat a section of code a specific number of times, use a For...Next loop. The structure follows the pattern For [counter] = [start] To [end] ... Next [counter].
This macro executes the "Cells(i,1).value = i" instruction with i = one. When it reaches the line "Next i," the macro increases the value of i to two and executes the "Cells" line again. It repeats a third time with i = three, and finally with i = four.
The result:
Sometimes you don't know how many times to execute a loop, but instead wish to run the loop until a certain condition is met. To do this, use a Do...Until loop.
This code first checks whether the active cell is empty. If not, the code increments the value by one and moves the active cell selector down one row. Finally, it returns to the top of the loop and tests whether the new cell is empty, repeating the loop until it reaches an empty cell.
Beginning with these numbers, if you select cell B3 and begin the macro, it will produce:
Each cell value is increased by one, until the macro reaches cell B13, an empty cell, and stops.
Similar to the Do . . . Until loop, the Do . . . While loop executes the code while a condition is true.
The difference between Do . . . Until and Do . . . While is subtle, yet simple:
For even more power, try running a loop inside a loop. This example combines a For loop (to iterate through rows) with a Do...Until loop (to iterate through columns until a blank cell is found), allowing you to process a two-dimensional range.
This loops through the first 30 rows of the spreadsheet. On each row, it begins in column A and increments the cell value by one until it reaches a blank cell.
Once your loops are working correctly, a few adjustments can make them run significantly faster. These tips are especially useful when your macro processes large datasets with hundreds or thousands of rows.
1. Turn Off Screen Updating
Add Application.ScreenUpdating = False before your loop and set it back to True after the loop finishes. This prevents Excel from refreshing the display on every iteration, which can dramatically reduce macro running time.
2. Set Calculation to Manual
Use Application.Calculation = xlCalculationManual before the loop and restore it with xlCalculationAutomatic afterward. This stops Excel from recalculating every formula each time a cell value changes inside the loop.
3. Use Specific Data Types
Declare your counter variables as Long instead of Variant. Specific data types use less memory and process faster, which adds up over thousands of iterations.
4. Read Data into an Array
Instead of reading and writing cell by cell, load your range into a VBA array, process the array in memory and write the results back to the range in a single operation. This approach can cut execution time from minutes to seconds on large datasets.
5. Avoid Select and Activate
Referencing cells directly with Cells(i, 1).Value is much faster than using Range("A1").Select followed by ActiveCell.Value. Every Select or Activate call forces Excel to update the interface, which slows your loop.
6. Move Static Operations Outside the Loop
If a calculation or variable assignment produces the same result on every iteration, move it above the loop. Only include operations inside the loop body that genuinely need to repeat.
Even experienced VBA users run into loop problems from time to time. Here are the most common mistakes and how to address them.
Loops give your code the power to repeat actions. With the variety of loops available in Excel, your code can adapt to any conditions, even when you don't know ahead of time whether your code needs to run one time or a thousand.
In this article you walked through four loop structures: For...Next for a known number of iterations, Do...Until and Do...While for condition-based repetition and nested loops for processing multi-dimensional ranges. Combined with the performance tips and troubleshooting guidance above, you have a solid foundation for writing efficient, reliable Excel macro loop code.
Mastering VBA loops is a skill that pays off every time you open a spreadsheet. If you are ready to go deeper, Pryor Learning offers live virtual and In-Person seminars covering Excel VBA and other Microsoft Office skills. Explore Pryor Learning's Excel training courses for hands-on instruction with expert trainers who can help you build on what you have learned here.