Smart Systems for Home

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

Excel VBA Multidimensional Arrays: Step-by-Step

· 7 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

A multidimensional array in Excel VBA is a single variable that holds a grid of values instead of a single row—up to 60 dimensions are allowed, though two or three are what you will actually use. You declare it with commas between bounds (Dim sales(1 To 12, 1 To 5)), fill it with nested For loops, and read any cell with sales(month, region). To get the total, loop with LBound and UBound rather than hard-coding numbers, then paste the whole block into a worksheet with one Range.Value = assignment.

What You'll Need

  • Excel on the desktop (Windows or Mac)—VBA is not available in Excel for the web or in the mobile apps.
  • A workbook saved as .xlsm (macro-enabled) so your code survives closing the file.
  • Access to the VBA editor: press Alt+F11 on Windows, or Option+F11 on Mac. On newer Mac builds you may also find it under Tools > Macro > Visual Basic Editor.
  • Macro security set to allow your code to run. If the Developer tab is hidden, right-click the ribbon and choose Customize the Ribbon to show it.
  • A short data table on a sheet to practice on—say months down the side and a few regions across the top.

Menu names in Excel and the VBA editor vary slightly by version, platform and region, so if a label on your screen is worded a little differently, look for the closest match.

Step-by-Step

  1. Open the editor and insert a module. Press Alt+F11, then in the Project pane right-click your workbook name and choose Insert > Module. Code in a standard module is easy to run with F5; code in a sheet module only fires on that sheet's events.

  2. Declare the array with sized bounds. Fixed size looks like this:

    Dim sales(1 To 12, 1 To 3) As Double

    The first number is months, the second is regions. A fixed array keeps all its values; a dynamic array declared as Dim sales() As Double is emptied whenever you resize it with ReDim, which catches many people out.

  3. Fill it with nested loops. Outer loop walks the rows, inner loop walks the columns:

    Dim m As Long, r As Long
    For m = 1 To 12
        For r = 1 To 3
            sales(m, r) = Cells(m + 1, r + 1).Value
        Next r
    Next m

    Note the order: sales(row, column). Reversing it does not raise an error if both bounds are the same size—it silently gives you transposed answers.

  4. Read a single element by its two indexes. Debug.Print sales(3, 2) prints March for region 2 into the Immediate window (Ctrl+G). Indexes are independent: sales(3, 2) and sales(2, 3) are different cells.

  5. Loop with LBound and UBound instead of literals.

    For m = LBound(sales, 1) To UBound(sales, 1)
        For r = LBound(sales, 2) To UBound(sales, 2)
            total = total + sales(m, r)
        Next r
    Next m

    The second argument to LBound/UBound is the dimension number. This keeps working when you resize the array later.

  6. Resize a dynamic array when the data size is unknown. Read the last used row, then ReDim:

    Dim data() As Variant, lastRow As Long
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    ReDim data(1 To lastRow - 1, 1 To 3)

    If the array already holds values you want to keep, use ReDim Preserve—but it only preserves the last dimension, and only that dimension can change size. Plan your layout so the growing dimension sits last.

  7. Write the whole array back in one go. Build a 1-based two-dimensional Variant array sized rows × columns and assign it directly:

    Range("F2").Resize(UBound(out, 1), UBound(out, 2)).Value = out

    One assignment is far faster than a loop of cell writes. Keep the array 1-based (1 To n) for this: a 0-based array written this way shifts every value up and left by one row and column.

  8. Clean up and test. Add Erase sales for a dynamic array when you are done, set the workbook calculation to manual while testing heavy loops, and step through with F8 to watch values in the Locals window.

Common Mistakes

Confusing dimensions with the Dim statement's commas. Dim a(10, 10) is one two-dimensional array holding 121 elements; Dim a(10), b(10) is two separate one-dimensional arrays. Also remember fixed arrays default to 0-based unless you write Option Base 1 at the top of the module—which affects every array in that module, so use explicit 1 To n bounds instead.

Exceeding the 60-dimension limit. VBA allows a maximum of 60 dimensions. A declaration like Dim a(1,1,1,1,1,1,1,1,1,1,...) with more than 60 commas errors at compile time. If you need more, you almost always need a different structure, not more dimensions.

Assuming ReDim Preserve keeps everything. It throws away all but the last dimension's data. Redeclaring a static array with ReDim is a compile error; only dynamic arrays can be resized.

Mixing up row and column in the assignment. When you paste an array with .Value, the first dimension must be rows and the second columns, matching the target range's shape. A mismatch either errors or produces a #N/A spill of wrong values.

Forgetting the data type. An untyped array is Variant; putting a string into a Double array raises run-time error 13 (type mismatch). Read text columns into a Variant or String array.

Tips

  • For a quick transpose, Application.WorksheetFunction.Transpose flips rows and columns of a one- or two-dimensional array—handy when your source data runs the other way.
  • Use a named constant for dimensions (Const REGIONS As Long = 3) so you change one number, not every loop.
  • Keep the sheet read and the sheet write outside the loops: read a range into a Variant in one line, process in memory, write back once. That is the main reason multidimensional arrays are worth the effort.
  • Add Option Explicit at the top of the module so a mistyped array name is caught at compile time rather than silently treated as a new empty variable.

Troubleshooting

Run-time error 9, subscript out of range. An index is outside the declared bounds. Print LBound and UBound for each dimension into the Immediate window and check your loop limits—especially if you mixed 0-based and 1-based arrays in one procedure.

Run-time error 13, type mismatch. You are writing text into a numeric array, or you declared the array without a type and are treating it as one. Check the cell contents with IsNumeric before assigning, or change the declaration to Variant.

Values appear shifted by one row or column. You copied a 0-based array into a range. Rebuild it as 1 To n, or place the values with a loop rather than a bulk assignment.

Only the last row of values appears after resizing. You used ReDim Preserve and the dimension that grew was not the last one. Reorder the array so the changing dimension is last, or collect results in a collection first.

Nothing happens when you press F5. Make sure the cursor is inside a Sub that takes no arguments. If you are prompted to enable macros, allow content for this file; if the workbook is blocked, check File > Info and the Trust Center's macro settings.

It runs but is slow. Turn off screen updating and calculation at the start of the procedure and restore them at the end, and replace per-cell writes with one array assignment.

Frequently Asked Questions

How many dimensions can a VBA array have?

Up to 60. In practice, two or three cover nearly every spreadsheet task; if you need more, a user-defined type or a class is usually clearer.

What is the difference between a static and a dynamic array?

A static array has its bounds fixed in the Dim statement and keeps its contents. A dynamic array is declared with empty parentheses and sized later with ReDim.

Does ReDim Preserve keep all my data?

Only the data in the last dimension. All other dimensions are reset, and only the last dimension may change size.

Can I write a two-dimensional array straight to a worksheet?

Yes, if it is a 1-based Variant array—assign it to a range of matching shape using .Value. Other data types need a loop or a Transpose step.

Why does my second index behave like the first?

You likely swapped rows and columns in the loops or in the write-back. VBA stores them in the order you declare—first dimension is rows.

Is Option Base 1 a good idea?

It changes every array in that module and confuses anyone reading the code later. Explicit 1 To n bounds are safer.