VBA FOR NEXT loop in Excel

The 'For Next' loop statement offers a way to execute a script for a given number of times thus reducing the code size.

The 'For Next' is a Control flow statement and is widely known as "For loop".

This statement comes to rescue when data from a large number of Cells has to be read or manipulated in Excel.

Example: The following example iterates 10 times and stores the value in an Array.

  1. Sub for_loop()
  2. Dim i As Integer
  3. Dim j(10) As Integer
  4. For i = 1 To 10
  5. j(i) = i
  6. MsgBox j(i)
  7. Next i
  8. End Sub


  1. For var_name=1 To 10 Step 1
  1. Next var_name

The For loop always ends with "Next" Keyword followed by variable_name.

The "Step" keyword defines how much to increase on each iteration, The default is 1.

A negative "Step" indicates iteration in Decreasing order.

Editor screenshot

excel vba for loop


A Messagebox is displayed 10 times from 1 to 10

excel vba for loop

excel vba for loop


You can find similar Excel Questions and Answer hereunder

1) How can I loop through all WorkSheets and get their names using VBA?

2) How to do a nested loop in VBA or a loop in a loop

3) How can I hide all comments in my WorkSheet using VBA?

4) How to do conditions in VBA with if then else

5) How to go through each item of an array, table, sheet with the for each function

6) How can I update a listbox based on data in a list using VBA?

7) Is there a way I can round the sales figure to the nearest 500?

8) How can I ensure that important files always appear on the recent Excel workbooks list?

9) Declaring variable and datatypes in Excel

10) Millions thousands custom number formatting in Excel


Here the previous and next chapter