Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: How to Get the Length of an Array

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To get the length of an array in Excel VBA, use the UBound and LBound functions. The count of elements is UBound(myArray) - LBound(myArray) + 1. There is no built-in "Length" property like in other languages, so this simple formula is the standard way to find out how many items an array holds, whether it starts at index 0, 1, or any other lower bound.

What You'll Need

  • Excel (any recent desktop version for Windows or Mac) with macros enabled
  • The VBA editor, opened with Alt + F11 (Windows) or Tools > Macro > Visual Basic Editor (Mac)
  • A workbook saved as a macro-enabled file (.xlsm) if you want to keep the code

Step-by-Step

  1. Open Excel and press Alt + F11 to open the Visual Basic for Applications editor.
  2. In the editor, go to Insert > Module to add a standard code module to the workbook.
  3. Declare your array in a Sub procedure. For example: Sub ArrayLength()
    Dim myArray(1 To 10) As String
    — this creates an array with 10 elements.
  4. Calculate the length with the standard formula: Dim n As Long
    n = UBound(myArray) - LBound(myArray) + 1
  5. Display or use the result. To check it quickly, add Debug.Print n and press Ctrl + G to open the Immediate Window, or use MsgBox n to show it in a pop-up.
  6. Run the macro by pressing F5 with the cursor inside the Sub, or press Alt + F8 in Excel, select ArrayLength, and click Run.
  7. For a dynamic array filled at runtime (for example with Split), declare it without bounds and let VBA size it: Dim parts() As String
    parts = Split("a,b,c", ",")
    MsgBox UBound(parts) - LBound(parts) + 1
    — this shows 3. Note that Split returns a zero-based array, so UBound alone gives 2; the full formula gives the true count.
  8. Optionally, wrap the logic in a reusable function so you can call it anywhere in your project:
    Function ArrayCount(arr As Variant) As Long
        ArrayCount = UBound(arr) - LBound(arr) + 1
    End Function
  9. If the array might be empty or not yet initialized, guard the call with If Not IsArray(arr) Then and an error handler, because calling UBound on an unallocated dynamic array raises runtime error 9 (Subscript out of range). A safe pattern is: On Error Resume Next
    n = UBound(arr) - LBound(arr) + 1
    If Err.Number <> 0 Then n = 0
    On Error GoTo 0

Common Mistakes

  • Using UBound alone as the length. UBound returns the highest index, not the count. For a zero-based array with 5 items, UBound is 4.
  • Assuming every array starts at 0. Arrays filled from a worksheet Range always start at 1, and Option Base 1 changes the default lower bound. Always use the UBound - LBound + 1 formula so it works either way.
  • Calling UBound on an uninitialized dynamic array, which throws runtime error 9. Always dimension the array (with ReDim or an assignment) before measuring it.
  • Trying to apply UBound to a non-array variable such as a single Variant holding a string. Check with IsArray first when the type is uncertain.
  • Forgetting the second argument for multi-dimensional arrays. UBound(arr) returns the upper bound of the first dimension only; use UBound(arr, 2) for the second dimension.

Tips

  • Use Long rather than Integer for the result variable so very large arrays never overflow.
  • Build the small ArrayCount helper function once and store it in your Personal Macro Workbook, then reuse it across all projects.
  • For 2-D arrays, the total element count is (UBound(arr,1) - LBound(arr,1) + 1) * (UBound(arr,2) - LBound(arr,2) + 1).
  • When reading a range into a Variant (for example vals = Range("A1:A100").Value), remember the result is always a 2-D array even for a single column, so query dimension 1 for the row count.
  • Test array bounds with Debug.Print in the Immediate Window before relying on them in loops — an off-by-one error is the most common loop bug in VBA.

Troubleshooting

  • Runtime error 9, "Subscript out of range": the array has not been allocated yet. Add a ReDim statement or assign the array (for example from Split or a range) before calling UBound, or add the error-guard pattern from step 9.
  • Compile error "Expected array": the variable is not declared as an array. Declare it with parentheses, as in Dim arr() As String, or make the parameter a Variant and check with IsArray.
  • The count is one too high or one too low: you likely used UBound or LBound alone. Replace it with UBound - LBound + 1.
  • Nothing prints when testing: the Immediate Window may be hidden. Press Ctrl + G in the VBA editor to show it, then run the macro again.
  • Macros will not run: check that the file is saved as .xlsm and that macros are enabled. If Excel blocks them, save, close, and reopen the file and choose Enable Content when prompted, provided you trust the workbook.

Frequently Asked Questions

Is there a built-in Length property for arrays in VBA?

No. VBA arrays do not have a Length or Count property. The standard approach is UBound(array) - LBound(array) + 1. Collections and scripting Dictionary objects do have a Count property, but plain arrays do not.

What is the difference between UBound and the array length?

UBound returns the highest index. Length is the number of elements. For a zero-based array with 10 items, UBound is 9 and the length is 10. Only when LBound is 0 does UBound + 1 equal the length.

How do I get the length of a 2-D array?

Query each dimension separately: rows = UBound(arr, 1) - LBound(arr, 1) + 1, columns = UBound(arr, 2) - LBound(arr, 2) + 1. Multiply them for the total number of elements.

Does Option Base 1 affect the formula?

No. Because the formula subtracts LBound, it gives the correct count whether arrays default to 0 or 1. That is exactly why the full formula is safer than UBound alone.

How do I check if an array is empty?

The most reliable way is to attempt UBound inside an error handler: if error 9 is raised, the array is uninitialized and effectively empty. There is no dedicated IsEmpty test for arrays.