Smart Systems for Home

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

Excel VBA Create Array from Range: Step-by-Step

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

Yes, and it takes one line of code: assigning a Range directly to a Variant variable copies all its cells into a 2D array instantly, which is far faster than reading cells one at a time. This guide shows you how to create that array, work with it, and write it back to the sheet.

What You'll Need

  • Excel for Windows or Mac with macros enabled (a workbook saved as .xlsm, or work in a fresh unsaved file for testing)
  • The Developer tab visible in the ribbon (if it isn't, enable it via File > Options > Customize Ribbon and tick Developer in the right-hand list — exact wording can differ slightly between versions)
  • Basic familiarity with opening the VBA editor (Alt + F11 on Windows, or Tools > Macro > Visual Basic Editor on Mac)

Step-by-Step

  1. Open the VBA editor. Press Alt + F11, or go to the Developer tab and click Visual Basic.
  2. Insert a module. In the editor, choose Insert > Module. A blank code window appears.
  3. Start a new sub. Type the following to begin:
Sub RangeToArray()

End Sub
  1. Declare a Variant variable. The variable must be a plain Variant, not a typed array like String(). Add this inside the sub: Dim arr As Variant
  2. Assign the range in one line. Point the Variant at the range you want. For example:
arr = Range("A1:C10").Value

That single line is the whole trick. Excel copies every cell's value into arr as a 2D array with the same rows and columns as the range. Reading it this way is dramatically faster than looping through cells.

  1. Reference the range more safely. To avoid depending on which sheet happens to be active, qualify the range with its worksheet: arr = ThisWorkbook.Worksheets("Sheet1").Range("A1:C10").Value. You can also use a named range: arr = Range("MyData").Value.
  2. Handle a dynamic size. If the data grows over time, capture the whole used block with CurrentRegion: arr = Worksheets("Sheet1").Range("A1").CurrentRegion.Value. This grabs every contiguous row and column touching A1, including any header row.
  3. Read values from the array. The array VBA builds from a range is always 2D, indexed from 1, as arr(row, column). For example:
Debug.Print arr(1, 1)      ' value from A1
Debug.Print arr(2, 3)      ' value from C2

Dim r As Long, c As Long
For r = LBound(arr, 1) To UBound(arr, 1)
    For c = LBound(arr, 2) To UBound(arr, 2)
        Debug.Print arr(r, c)
    Next c
Next r
  1. Write the array back if needed. Arrays created from ranges work in reverse too. To dump modified values back to the sheet in one shot, assign the array to a same-sized range: Range("E1").Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr. The target range must exactly match the array's dimensions, which Resize handles for you.
  2. Run and test. Press F5 with the cursor inside the sub, then open the Immediate window (Ctrl + G in the editor) to see the Debug.Print output.

Common Mistakes

  • Forgetting .Value. Writing Set arr = Range("A1:C10") stores a Range object, not an array. Without Set but also without .Value, older code often still works because .Value is the default property, but be explicit: arr = Range("A1:C10").Value.
  • Assuming a 1D array. Even a single column or single row of cells becomes a 2D array. arr(5) fails; you need arr(5, 1).
  • Wrong lower bound. Arrays from ranges start at index 1, not 0, even if your module declares Option Base 0. Use LBound and UBound in loops rather than hard-coding numbers.
  • Using a typed array. Dim arr() As String cannot receive a range dump, because cells can hold numbers, text, errors and dates at once. Use Variant.
  • Assigning a single cell. arr = Range("A1").Value gives a plain Variant value, not an array, so UBound fails. Single-cell reads don't need an array at all.
  • Writing back to a mismatched range. Assigning a 10x3 array to a 5x3 range throws an error. Always Resize to match UBound.

Tips

  • Check whether you actually have an array with IsArray(arr) before looping — useful when the range might be a single cell.
  • Get row and column counts cleanly: rowCount = UBound(arr, 1) - LBound(arr, 1) + 1 (same idea for dimension 2).
  • Read .Value2 instead of .Value if your data is numeric only; it's marginally faster because dates and currencies come in as plain doubles.
  • Do all your calculations inside the array and write back once at the end. Touching the worksheet inside a loop is the single biggest cause of slow macros.
  • Turn off ScreenUpdating (Application.ScreenUpdating = False) around large write-backs, and set it back to True when done.

Troubleshooting

  • Run-time error 9, subscript out of range. You're indexing past UBound or using one index on a 2D array. Print LBound and UBound for both dimensions to see the real bounds.
  • Run-time error 13, type mismatch. Usually a typed array or trying to loop a Variant that holds a single value rather than an array. Confirm the range has more than one cell and declare the variable as Variant.
  • Empty values everywhere. You referenced a range on the wrong sheet. Qualify with Worksheets("SheetName") and check the sheet name matches exactly, including spaces.
  • The macro does nothing / buttons greyed out. The workbook is probably saved as .xlsx with macros stripped, or macros are disabled. Save as .xlsm and enable macros via the security warning bar or Trust Center settings.
  • Numbers come in as text. The cells themselves contain text-formatted numbers. Fix the source formatting (select the cells, set the format to General, then re-enter or use Text to Columns) — the array faithfully copies whatever the cells hold.

Frequently Asked Questions

Can I create a 1D array from a single column?

Not directly — a range always produces a 2D array, even for one column. Loop once through the rows and copy into a separate 1D array, or use Application.Transpose on the column before assigning (reliable for values, with a length limit on very long columns).

Is reading a range into an array really faster?

Yes, substantially. Each cell read is a separate call between VBA and the worksheet; one bulk assignment avoids that overhead entirely, which matters most on large ranges.

Does this work on Excel for Mac?

Yes. Range-to-array assignment is core VBA and behaves the same on Mac; only the editor shortcuts and menus differ.

How do I get just one column of the range into its own array?

Either assign the column's range directly (arr = Range("B1:B10").Value) or loop through the full array's second dimension and copy the values you want.

Will formulas be copied into the array?

No. .Value returns calculated results, not formulas. Use .Formula on the range if you want the formula text instead.

What happens to blank cells and error values?

Blank cells arrive as Empty (test with IsEmpty), and cell errors like #N/A arrive as Error values (test with IsError). Handle both before doing math on the array.