30 Advanced Excel Tips for 2026

Excel Champs / VBA / Arrays / VBA Loop Through an Array
By Puneet Gogia (Microsoft MVP)

VBA Loop Through an Array

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

Loop from LBound to UBound so the code covers every element, whatever the array’s size or starting index:

Dim i As Long
For i = LBound(myArray) To UBound(myArray)
    myArray(i) = myArray(i) * 2
Next i

To read each value without changing it, For Each is shorter: Dim v As Variant, then For Each v In myArray … Next v. It can’t change the elements, because assigning to v changes only the copy, so use the counter loop above to update values.

An array filled from a range is two-dimensional, so loop through rows and columns with UBound(arr, 1) and UBound(arr, 2), and refer to each element as arr(r, c).

Don’t hard-code the limits. Dim myArray(5) has six elements, 0 to 5, unless the module starts with Option Base 1, so a loop written as For i = 1 To 5 silently skips element 0. LBound and UBound always match the array.

In VBA, to loop through an array you can use the For Loop (For Next). And to write this loop code you need to know the size of the array (upper and lower bound) so that you can use both as the counter for the loop. Basically, the For loop will start from the first element of the array and loop up to the last.

You have the following array with five elements from 1 to 5 and in these elements, you have numbers. So, the idea is to write a code that can loop through all these five elements and then multiply each one by 2.

vba-loop-through-an-aray-1

Loop Through an Array in VBA

  1. First, you need to know the upper and lower bounds of the array and for this, you need to use the UBOUND and LBOUND functions.
  2. After that, you need to use these bounds to create a counter for the loop.
  3. Next, you need to write a line of code to multiply the element’s value by 2. This line of code will take value from the element itself and then multiply it.
  4. In the end, use the “Next” keyword to close the loop code.

Here’s the full code.

Option Base 1
Sub vba_array_loop()
   
Dim myArray(5) As Integer
myArray(1) = 10
myArray(2) = 20
myArray(3) = 30
myArray(4) = 40
myArray(5) = 50

Dim uB As Integer, lB As Integer
uB = UBound(myArray)
lB = LBound(myArray)

For i = lB To uB
    myArray(i) = myArray(i) * 2
Next i
   
End Sub

The “Option Base 1” line goes at the very top of the module, before any procedure. It makes the array start from 1, so myArray(5) has five elements from 1 to 5. Without it, the array starts from 0 and myArray(5) has six elements (0 to 5). The loop still works either way, because it takes the counter from LBound and UBound and not from fixed numbers.

This code doesn’t show anything on the screen. To see the new values, add the line below inside the loop (after the line that multiplies) and open the Immediate Window from View > Immediate Window (Ctrl + G) before you run the code.

Debug.Print myArray(i)

Loop Through an Array with For Each

You can also use the For Each loop. Here you don’t need the upper and lower bounds at all, as the loop goes through each element one by one. The only rule is that the variable you use for the element must be a Variant.

Sub vba_array_for_each()
Dim myArray As Variant
Dim element As Variant
myArray = Array(10, 20, 30, 40, 50)
For Each element In myArray
Debug.Print element * 2
Next element
End Sub

There’s one thing you need to know here. For Each is good for reading values, but it can’t change them. The element variable holds a copy of the value, so a line like element = element * 2 doesn’t change the array. When you want to change the values, use the For Next loop with LBound and UBound, as in the code above. Also, For Each doesn’t work with an array of user-defined types.

Loop Through an Array Backward

To go from the last element to the first, start the counter from UBound, end it at LBound, and use Step -1.

Sub vba_array_loop_backward()
Dim myArray As Variant
Dim i As Long
myArray = Array(10, 20, 30, 40, 50)
For i = UBound(myArray) To LBound(myArray) Step -1
Debug.Print myArray(i)
Next i
End Sub

Loop Through a Two-Dimensional Array

For a multi-dimensional array, you need one loop for each dimension, one inside the other. LBound and UBound take a second argument for the dimension: 1 for the rows and 2 for the columns. If you skip it, VBA uses the first dimension.

Sub vba_2d_array_loop()
Dim myArray(1 To 3, 1 To 2) As Integer
Dim r As Long, c As Long
For r = LBound(myArray, 1) To UBound(myArray, 1)
For c = LBound(myArray, 2) To UBound(myArray, 2)
myArray(r, c) = r * c
Debug.Print r, c, myArray(r, c)
Next c
Next r
End Sub

Loop Through an Array from a Range

When you store a range in an array, VBA always creates a two-dimensional array that starts from 1, even for a single column. So, you need to refer to each element with its row and column. The code below takes the numbers from A1:A5, multiplies each one by 2, and writes the results to B1:B5 in one go.

Sub vba_range_array_loop()
Dim myArray As Variant
Dim i As Long
myArray = Range("A1:A5").Value
For i = LBound(myArray, 1) To UBound(myArray, 1)
myArray(i, 1) = myArray(i, 1) * 2
Next i
Range("B1:B5").Value = myArray
End Sub

Looping through an array like this is much faster than looping through the cells one by one, as the code reads and writes the worksheet only once. If any cell in A1:A5 has text, the multiplication gives a Type mismatch error.

Exit the Loop Early

When you are looking for a value, there’s no need to go through the rest of the array once you find it. Use Exit For to stop the loop right there.

Sub vba_array_find_value()
Dim myArray As Variant
Dim i As Long
myArray = Array(10, 20, 30, 40, 50)
For i = LBound(myArray) To UBound(myArray)
If myArray(i) = 30 Then
MsgBox "Found 30 at index " & i
Exit For
End If
Next i
End Sub

Which Method to Use

  • For Next with LBound and UBound: Use it when you want to change the values, need the index of each element, or want to loop backward.
  • For Each: Use it when you only want to read the values and don’t need the index.
  • Nested For Next: Use it for two-dimensional arrays, including an array that you get from a range.

Common Mistakes

  • Using fixed numbers for the counter: A loop like For i = 1 To 5 breaks when the array starts from 0 or changes size. Use LBound and UBound instead.
  • Subscript out of range (run-time error 9): This comes when the counter goes past the bounds of the array, or when you use UBound on a dynamic array that you haven’t sized with ReDim yet.
  • Changing values with For Each: The values in the array stay the same. Use For Next for that.
  • Treating a range array as one-dimensional: An array from a range needs two indexes, like myArray(i, 1), even for one column.

Frequently Asked Questions

Can I loop through an array without LBound and UBound?

Yes. The For Each loop goes through every element without any bounds. But it can only read the values, not change them, and the loop variable must be a Variant.

Why do I get “Subscript out of range” in my loop?

The counter is going below the lower bound or above the upper bound of the array, usually because the loop uses fixed numbers. Use LBound and UBound for the counter. You also get this error when the array is a dynamic array that hasn’t been sized with ReDim yet.

How do I loop through an array of strings?

The same way as an array of numbers. The loop doesn’t care about the data type, so you can use For Next with LBound and UBound or For Each. You can learn more about it in VBA Array with Strings.

How do I see the values while the loop runs?

Add a Debug.Print line inside the loop and open the Immediate Window with Ctrl + G. You can also press F8 to run the code one line at a time.

Puneet Gogia

Written by

Puneet Gogia

Microsoft MVP — Excel · Founder, Excel Champs

Twelve years of teaching Excel, and a decade before that using it as a data analyst. Every tutorial here is written from a file I actually built.

3M+ readers helped
1,000+ tutorials written
Since 2015 teaching Excel