Excel VBA: Assign an Array to a Range
In this article
You assign an array to a range in Excel VBA by sizing a two-dimensional Variant array to match the range, filling it, and then writing it back with a single statement such as Range("A1:C10").Value = myArray. The reverse direction works the same way: reading Range("A1:C10").Value into a Variant gives you a 2D array, indexed from 1, that you can loop through far faster than cell-by-cell reads. This single-line transfer is the fastest reliable way to move bulk data between VBA and the worksheet, and it is the technique behind most "why is my macro slow" fixes.
What You'll Need
- Excel on Windows or Mac with the Visual Basic Editor available (it ships with desktop Excel; Excel for the web has no VBA).
- A workbook saved as .xlsm or .xlsb so macros are preserved. A plain .xlsx will strip your code on save.
- Macro security set to allow macros, typically through File > Options > Trust Center > Trust Center Settings > Macro Settings. Labels shift slightly between versions, so match on meaning rather than exact wording.
- A test worksheet with a small block of cells, for example A1:C10, so you can see the write happen before running it on live data.
Step-by-Step
- Open the editor and insert a module. Press Alt+F11 (Option+F11 on Mac) to open the Visual Basic Editor, then choose Insert > Module. Code goes in a standard module, not in a sheet or ThisWorkbook module, unless you specifically need event code.
- Declare the array and the range dimensions. Ranges map to two-dimensional arrays: rows by columns. Declare a Variant and a pair of dimension variables:
Variant is the practical choice here because only a Variant array can be assigned to a range in one statement.Dim data As Variant Dim rows As Long, cols As Long Dim target As Range - Point at the target range and size the array to it. Use
Set target = Range("A1:C10"), thenrows = target.Rows.Countandcols = target.Columns.Count. Sizing from the range rather than hard-coding numbers means the macro keeps working if the block grows or shrinks. - Size the array from 1, not 0. Ranges expect a 1-based array.
ReDim data(1 To rows, 1 To cols)is correct. A defaultDim data(9, 2)starts at zero and is the single most common cause of a garbled or shifted write. - Fill the array with a loop before writing. For example:
Fill every element. Empty elements write as blanks over whatever was in the cell, which can silently erase data.Dim r As Long, c As Long For r = 1 To rows For c = 1 To cols data(r, c) = r * 10 + c Next c Next r - Write the array in a single statement.
target.Value = datadoes the whole job. Use.Value2instead if your data contains dates and you want the underlying serial numbers rather than formatted date values. Do not loop cell by cell to write; that defeats the purpose. - Test on a scratch sheet. Run the macro with F5, then confirm the shape matches: 10 rows and 3 columns landing in A1:C10, with the first value in A1 and the last in C10. If the block is offset by one cell, your array lower bound is wrong.
The reading direction is worth learning in the same sitting, because it is the same mechanics in reverse. data = Range("A1:C10").Value produces a 1-based, two-dimensional Variant array with rows in the first dimension and columns in the second. You can then compute over it in memory and write results back with Range("E1:G10").Value = results. Reading a large block once, processing it in a loop, and writing once is dramatically faster than touching individual cells, because each worksheet read or write crosses a boundary that costs far more than an in-memory array access.
Common Mistakes
Using a zero-based array. Dim data(9, 2) gives indices 0 to 9 and 0 to 2, but a range expects 1 to 10 and 1 to 3. Always declare with an explicit 1 To lower bound when the array goes to a sheet.
Declaring the array as a specific type. Dim data() As Double will not assign to a range in one step. Ranges require Variant arrays containing Variant elements. Fix the declaration, not the assignment.
Shape mismatch. If the array is 10 by 3 and the target is 12 by 3 (or you used Application.Transpose and flipped the axes), the write either fails with a type mismatch or splashes values across the wrong cells. Verify rows and columns in the Immediate Window with Debug.Print UBound(data, 1), UBound(data, 2) before writing.
Writing over existing data. Assigning to a range replaces every cell in it, including the ones your array left empty. Clear or back up the area first, or size the range to exactly the filled portion.
Spilling a 1D array. A one-dimensional array assigned to a multi-cell range behaves unexpectedly on older versions and can repeat values. Convert to 2D explicitly, for example by building data(1 To n, 1 To 1) for a single column.
Tips
Use Application.ScreenUpdating = False and Application.Calculation = xlCalculationManual around bulk writes, then restore both, so Excel does not repaint or recalculate after every statement. Wrap the restore in an error handler so a crash does not leave calculation switched off.
Pull data with Range("A1").CurrentRegion when the block has no gaps, which avoids counting rows manually. For large imports, write to an Application.Transpose-free 2D layout rather than converting orientations, since Transpose has practical size limits and will fail on very large arrays.
Keep the array local and the range explicit. Working against a named worksheet object (ws.Range(...) rather than Range(...)) prevents the macro from writing into whichever sheet happens to be active.
Troubleshooting
If you get "Type mismatch", check three things in order: the array is declared as Variant, it is two-dimensional, and it is 1-based. If the macro runs but writes to the wrong cells, confirm the target range dimensions equal the array dimensions and that no merged cells exist inside the range, because merged cells break block assignment. If values appear shifted right or down by one, your lower bound is 0. If dates arrive as serial numbers or as text, switch between .Value and .Value2 and check the cell format afterward. If the write is still slow, look for a loop that assigns cell by cell somewhere in the procedure; a single block assignment should feel instantaneous on a few thousand cells. If nothing happens at all, confirm the macro is in a standard module and that macros are enabled for the file.
Frequently Asked Questions
Can I assign a one-dimensional array to a range?
Not reliably. Ranges expect two dimensions, so reshape a single row or column as data(1 To n, 1 To 1) or data(1 To 1, 1 To n) before assigning.
Does the array have to be Variant?
To assign in one statement, yes. Typed arrays such as Long() or Double() cannot be handed to a range directly.
Why does my range assignment overwrite cells outside the data?
The write covers the entire target range, not just the cells your array filled. Trim the range to the array size, or fill every element before assigning.
Is assigning an array faster than a loop?
Substantially. One read and one write replace thousands of individual cell operations, which is why the technique is standard for bulk data work.
Can I write to multiple non-adjacent areas at once?
No single statement covers disconnected areas of differing shapes. Write each area separately, or consolidate the data into one contiguous block first.