Excel VBA: Write an Array to a Range (Fast Guide)
In this article
Writing a VBA array to a worksheet range in one assignment is the fastest way to move large blocks of data in Excel. Instead of looping cell by cell, you build a 2D Variant array, size the destination range to match, and assign the array to Range.Value in a single line. That single line can replace thousands of individual cell writes and cut run time from minutes to a fraction of a second. Before you start, you need Excel with the Developer tab enabled and a basic understanding of how to insert a module and run a macro. No add-ins are required.
What You'll Need
- Excel (any modern desktop version) with macro-enabled workbook format (.xlsm) if you intend to save your code.
- The Developer tab visible: right-click the ribbon and choose Customize the Ribbon, then tick Developer. Labels can vary slightly by version and region.
- A standard module: in the VBA editor, Insert > Module.
- Macros enabled for the workbook, or the code will not run.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (on Mac, use the Developer tab > Visual Basic). This opens the editor window.
- Insert a module. In the editor, go to Insert > Module. A blank code window appears. All the code below goes there.
- Decide the array shape. A range write needs a 2D array: rows in the first dimension, columns in the second. A 1D array will fail or write only the first element. Declare it as
Dim arr As Variantso it can hold mixed types. - Size the array. Use
ReDim arr(1 To rowCount, 1 To colCount). Both bounds should start at 1 to match the range, though 0-based arrays work if you offset correctly. Starting at 1 avoids off-by-one confusion. - Fill the array. Loop through your source data and assign to
arr(r, c). Keep the loop order matching the array dimensions: outer loop for rows, inner loop for columns. This is where you do any calculation, lookup, or formatting logic. - Size the destination range. Use
Worksheets("Sheet1").Range("A1").Resize(rowCount, colCount). The Resize method takes rows then columns, in that order. If the range size does not match the array, Excel raises a run-time error 1004. - Assign the array to the range. Write
destination.Value = arr. This is the actual write. Do not usedestination.Value2unless you specifically need to strip currency and date formatting; Value is the safer default. - Run the macro. Press F5 in the editor, or assign the macro to a button on the sheet. Check the destination range to confirm every element landed in the right cell.
- Optionally clear the destination first. If the new array is smaller than the previous data, stale values remain below or to the right. Call
destination.ClearContentsbefore writing, or clear a larger area.
Common Mistakes
- Using a 1D array. A single-dimension array assigned to a multi-cell range writes the first element to every cell or triggers an error. Always build a 2D array.
- Mismatched dimensions. If the array is 10 rows by 3 columns and the range is 3 rows by 10 columns, you get error 1004. Resize the range from the array's bounds, not from memory.
- Forgetting the second dimension. Writing
ReDim arr(1 To n)and then assigningarr(i, 1)causes a subscript out of range error. - Assigning to a single cell. If the destination is one cell, only the top-left element is written. Use Resize to cover the full array.
- Writing to a protected sheet. The assignment fails silently or errors. Unprotect the sheet first, or write to an unprotected area.
- Assuming the array is transposed. Excel writes rows to rows and columns to columns. If your data appears rotated, you built the array with dimensions swapped.
Tips
- Read a range into an array with
arr = Range("A1:C10").Value, process it in memory, then write it back. This round trip is the core of fast VBA data processing. - Turn off screen updating with
Application.ScreenUpdating = Falsebefore the write and back on after. It makes a visible difference on large writes. - Use
LBoundandUBoundto size the destination range from the array itself, rather than hard-coding counts. - For very large arrays, write to a new sheet or a blank area, then copy or move the result. This avoids partial overwrites if something fails mid-run.
- Keep the array as Variant. Declaring it as String or Double can cause type mismatches when the range contains blanks or errors.
Troubleshooting
If the macro errors with run-time error 1004, the most likely cause is a size mismatch between the array and the range. Check the array bounds and the Resize arguments side by side. If the error mentions "Application-defined or object-defined error", confirm the sheet name is spelled exactly as it appears in the tab and that the sheet is not protected.
If the data lands in the wrong cells, verify the array's first dimension is rows and the second is columns. A quick test is to write a small 2x3 array to a blank sheet and inspect where each value appears.
If nothing happens at all, the macro may not be enabled. Check the Trust Center macro settings, and confirm you are running the correct procedure from the correct module. If the workbook was downloaded, Windows may have blocked macros; open the file properties and unblock it if you trust the source.
If only the first value appears repeated across the range, you almost certainly used a 1D array. Rebuild it as 2D with explicit row and column bounds.
Frequently Asked Questions
Can I write a 1D array directly to a row or column?
Not reliably. Excel expects a 2D array for a multi-cell range write. Convert the 1D array to 2D first, or use Application.Transpose to reshape it before assigning.
Why is my array write slower than expected?
Check that you are not writing inside a loop. The speed comes from one assignment to the whole range. If you are still looping cell by cell, the array is not being used as intended.
Does the array need to start at index 1?
No, but it is simpler. If you use a 0-based array, the range write still maps the first element to the top-left cell, so the result is the same as long as the sizes match.
Can I write dates and formatted numbers this way?
Yes. The array holds the underlying values, and the destination cells apply their own number formats. If you need specific formatting, set it on the range before or after the write.
What happens if the destination range already has data?
The write overwrites exactly the cells covered by the resized range. Anything outside that area is left untouched, which is why clearing a larger area first is often wise.
Can I write an array to a range on another sheet?
Yes. Fully qualify the destination with the worksheet object, for example Worksheets("Results").Range("A1").Resize(r, c).Value = arr. The sheet does not need to be active.