30 Advanced Excel Tips for 2026

Excel Champs / VBA / Arrays / VBA Array Length (Size)
By Puneet Gogia (Microsoft MVP)

VBA Array Length (Size)

30+ Advanced Excel Tips for 2026

No spam. Only Excel Stuff. God Promise. 😊

The number of elements in a one-dimensional array is UBound(arr) - LBound(arr) + 1. Use both bounds rather than UBound alone, because an array can start at 0 or 1: Dim arr(5) has six elements, 0 to 5, and Dim arr(1 To 5) has five.

For a two-dimensional array, count each dimension and multiply. Rows are UBound(arr, 1) - LBound(arr, 1) + 1, and columns are UBound(arr, 2) - LBound(arr, 2) + 1. An array filled from Range("A1:C100").Value always starts at 1, so UBound(arr, 1) is the row count and UBound(arr, 2) the column count.

A dynamic array that hasn’t been sized with ReDim has no bounds yet, and UBound stops with error 9. This function returns 0 for one instead:

Function ArrayLength(arr As Variant) As Long
    On Error Resume Next
    ArrayLength = UBound(arr) - LBound(arr) + 1
    If Err.Number <> 0 Then ArrayLength = 0
End Function

IsEmpty can’t tell you whether an array has elements; it returns False for every array. Store counts in a Long, because an Integer overflows above 32,767.

In VBA, to get the length of an array means to count the number of elements you have in that array. For this, you need to know the lowest element and the highest element. So, to get this, you can use the UBOUND and LBOUND functions that return the upper bound and lower bound, respectively.

The length of each dimension is the upper bound minus the lower bound, plus 1. You need the +1 because both bounds count as elements. So an array declared as (1 To 12) has 12 elements, and one declared as (0 To 5) has 6.

Apart from that, you can also use COUNTA, which is a worksheet function. And in this tutorial, we will see both of the methods, so you can use any of them at your convenience.

Steps to Get the Size of an Array

Here we have an array that contains a list of months and sales quantity for each month.

vba-array-length
  1. Make sure to have an array declared properly with rows and columns.
  2. After that, declare two more variables (as we have a two-dimensional array) to store the length of each dimension of the array.
  3. Next, you need to use a formula where you have to use the Ubound function to get the upper bound and then Lbound to get the lower bound of the array. Subtract the lower bound from the upper bound and add 1.
  4. As you have a two-dimensional array, you need to get the bounds for both of the dimensions and set those values to the variables.
  5. In the end, multiply the lengths of both dimensions that you have got from the Ubound and Lbound.

Here’s the full code.

Dim yearSales(1 To 12, 1 To 2) As Integer
Dim iCount1 As Integer, iCount2 As Integer
   
iCount1 = UBound(yearSales, 1) - LBound(yearSales, 1) + 1
iCount2 = UBound(yearSales, 2) - LBound(yearSales, 2) + 1
   
MsgBox iCount1 * iCount2

Note: You must be wondering why we have used 12 rows when we have a total of 13 rows in the array that I shared with you at the start of the post.

We have used an array with 12 rows because the first row was a heading. Now, you might also want to check if the array has any elements before you count them. You could think of using an IF STATEMENT with the ISEMPTY function for this, but it doesn’t work. ISEMPTY only gives a meaningful result for a Variant variable, and for an array it always returns False, so it can’t tell you whether the array has zero elements.

An array declared with fixed bounds always has elements. The one that can have zero elements is a dynamic array that you haven’t sized with ReDim yet, and calling UBound on it gives you the “Subscript out of range” error (run-time error 9). So, in the code below, I have used a dynamic array and an IF statement that checks if UBound runs without an error before counting the elements.

Dim yearSales() As Integer
Dim iCount1 As Integer, iCount2 As Integer
Dim isAllocated As Boolean
ReDim yearSales(1 To 12, 1 To 2)
On Error Resume Next
iCount1 = UBound(yearSales, 1)
isAllocated = (Err.Number = 0)
On Error GoTo 0
If Not isAllocated Then
    MsgBox "This array has zero elements."
Else
    iCount1 = UBound(yearSales, 1) - LBound(yearSales, 1) + 1
    iCount2 = UBound(yearSales, 2) - LBound(yearSales, 2) + 1
    MsgBox "This array has " & iCount1 * iCount2 & " element(s)."
End If

Using COUNTA to Get the Length of the Array

As you know, an array is a bunch of elements that are structured in a single or multi-dimensional way, and you can use the COUNTA function (worksheet function) to count these elements in one go.

In the following code, you have used the same array that you declared earlier and then used a variable to store the element count returned by the function.

And as you can see, the result it has returned is 24, which is the count of the total number of elements that we have in the array.

Dim yearSales(1 To 12, 1 To 2) As Integer
iCount = WorksheetFunction.CountA(yearSales)
MsgBox iCount

There’s one thing that you need to take care of: this method won’t be ideal to use in all situations, so it’s always good to use the method that we discussed earlier.

COUNTA counts the elements that hold a value, not the slots the array has. In an Integer array like the one above, every element holds a value (0 by default), so you get the full count. But in a Variant array, the elements you haven’t filled yet are Empty, and COUNTA skips them. So the count can come out lower than the real size of the array.

You can also write code to check first if the declared array is not blank. As I explained above, ISEMPTY can’t do this check, so the code below tests the array with UBound before COUNTA counts the elements.

Dim yearSales() As Integer
Dim iCount As Long
Dim isAllocated As Boolean
ReDim yearSales(1 To 12, 1 To 2)
On Error Resume Next
iCount = UBound(yearSales, 1)
isAllocated = (Err.Number = 0)
On Error GoTo 0
If Not isAllocated Then
    MsgBox "This array has zero elements."
Else
    iCount = WorksheetFunction.CountA(yearSales)
    MsgBox "This array has " & iCount & " element(s)."
End If

Get the Length of a One-Dimensional Array

Most of the time, you will work with a one-dimensional array, and there you only need one UBound and one LBound. You don’t need to mention the dimension number, as VBA uses the first dimension by default.

Sub ArrayLength()
Dim myArray(1 To 5) As String
Dim iLength As Long
iLength = UBound(myArray) - LBound(myArray) + 1
MsgBox "This array has " & iLength & " element(s)."
End Sub

Don’t use UBound alone as the length. When you declare an array without the To clause, like Dim myArray(5), it starts from 0, so it has 6 elements, not 5. It only starts from 1 if you have Option Base 1 at the top of the module. Arrays that you create with the Array function always start from 0, and the ones you get from a range (see VBA Range to an Array) always start from 1. UBound – LBound + 1 gives you the right length in all these cases.

Count Elements in an Array with Any Number of Dimensions

If you need to get the size of arrays again and again, you can write a custom function for it. The function below works with one, two or more dimensions. It goes through the dimensions one by one, multiplies their lengths, and stops when there are no more dimensions (UBound gives an error at that point). It returns 0 for a dynamic array that you haven’t sized yet and for an empty array.

Function ArrayLength(arr As Variant) As Long
Dim d As Long, total As Long, n As Long
If Not IsArray(arr) Then Exit Function
total = 1
On Error GoTo NoMoreDims
Do
d = d + 1
n = UBound(arr, d) - LBound(arr, d) + 1
total = total * n
Loop
NoMoreDims:
If d = 1 Then
ArrayLength = 0
Else
ArrayLength = total
End If
End Function

Put this function in a standard module (Insert > Module) and call it from your code like this:

Sub TestArrayLength()
Dim yearSales(1 To 12, 1 To 2) As Integer
MsgBox ArrayLength(yearSales)
End Sub

For the same yearSales array, it returns 24. And when the loop stops, d – 1 is the number of dimensions the array has, so you can use the same idea to count dimensions.

Length of an Empty Array from Split or Array

There’s one more case where an array has zero elements. When you use the Split function on an empty string, or the Array function with nothing in it, VBA gives you an empty array. Here, UBound doesn’t give an error. LBound returns 0 and UBound returns -1, so the same formula gives you 0.

Sub EmptySplitLength()
Dim parts As Variant
parts = Split("", ",")
MsgBox UBound(parts) - LBound(parts) + 1
End Sub

So, when you check if an array is empty, check for both: an error from UBound (a dynamic array not sized yet) and a length of 0 (an empty array).

Which Method to Use

  • UBound and LBound: Use this in most cases. It gives you the exact size of each dimension, whatever the data type and whatever values the array holds.
  • COUNTA: Use this only when you want to count the elements that hold a value. It skips Empty elements in a Variant array.
  • Custom function: Use this when you work with arrays of different dimensions or with dynamic arrays that might not be sized yet.

Frequently Asked Questions

Is there a Length property for an array in VBA?

No. VBA arrays don’t have a Length or Count property like arrays in other languages. You need to use UBound – LBound + 1 for each dimension. A Collection does have a Count property, so if you only need to store items and count them, a Collection can be easier.

Why do I get “Subscript out of range” when I use UBound?

There are two common reasons. The first is a dynamic array that you haven’t sized with ReDim yet. The second is a dimension number that’s higher than the number of dimensions the array has, like UBound(myArray, 2) on a one-dimensional array. Both give you run-time error 9.

Why does UBound return -1?

UBound returns -1 for an empty array, like the one you get from Split on an empty string or from the Array function with no values. Its LBound is 0, so its length is 0.

Does the length change after ReDim Preserve?

Yes. After you resize a dynamic array with ReDim or ReDim Preserve, UBound returns the new upper bound, so run the length formula again after resizing. With ReDim Preserve, you can only change the last dimension of the array.

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