Smart Systems for Home

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

Excel VBA: Write an Array to a Worksheet

· 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

To write an array to a worksheet in Excel VBA, assign the array directly to a Range of the same size: Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr. This works for two-dimensional arrays and is far faster than looping cell by cell. For a one-dimensional array, use Application.Transpose or loop through the elements. The direct assignment works because VBA can transfer a whole array to a range in one operation, avoiding the slow read/write per cell.

What You'll Need

  • Excel with macros enabled (the Developer tab visible, or access to the VBA editor via Alt+F11).
  • A basic understanding of VBA variables and arrays.
  • A worksheet ready to receive the data (the examples use the active sheet).

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (on Windows) or Fn+Alt+F11 (on some laptops). In the Project Explorer, right-click your workbook and choose Insert > Module. This creates a blank module where you can paste code. Note that menu labels can differ slightly by Excel version and region.
  2. Declare your array. Decide whether you need a one-dimensional or two-dimensional array. For a table of rows and columns, use a 2D array. Example: Dim arr(1 To 3, 1 To 2) As Variant. The first dimension is rows, the second is columns. Using Variant is safest because Excel ranges expect Variant arrays when assigning.
  3. Fill the array with data. Assign values element by element, or read from a range. For example:
    arr(1, 1) = "Name": arr(1, 2) = "Score"
    arr(2, 1) = "Alice": arr(2, 2) = 95
    arr(3, 1) = "Bob": arr(3, 2) = 88
    You can also load an existing range into an array: arr = Range("A1:B3").Value. That creates a 2D Variant array with the same dimensions as the range.
  4. Determine the target range size. Use UBound to get the upper bounds of each dimension. For a 2D array declared as above, UBound(arr, 1) returns 3 (rows) and UBound(arr, 2) returns 2 (columns). If your array was loaded from a range, the bounds match the range's row and column counts.
  5. Write the array to the worksheet. Use the Range.Resize method to set the destination size, then assign the array:
    Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
    This writes the entire array in one operation. The top-left cell of the target range is the anchor; the array fills down and to the right.
  6. Handle one-dimensional arrays. A 1D array cannot be assigned directly to a multi-cell range in the same way. Two options: loop through the elements and write each to a cell, or use Application.Transpose to convert it to a 2D array (one column) and then assign. Example:
    Range("A1").Resize(UBound(arr)).Value = Application.Transpose(arr)
    Note that Transpose has a limit of about 65,536 elements and may fail on very large arrays. For large 1D arrays, a loop is safer.
  7. Run the macro. Press F5 while the cursor is inside the subroutine, or assign the macro to a button. Check the worksheet to confirm the data appears in the expected cells.
  8. Clear previous data if needed. If you run the macro multiple times, old values may remain outside the new array's footprint. Clear the destination area first with Range("A1").Resize(UBound(arr,1), UBound(arr,2)).ClearContents or clear a larger fixed range.

Common Mistakes

  • Dimension mismatch. If the target range size does not match the array dimensions, Excel raises a "Subscript out of range" or "Run-time error 13: Type mismatch". Always use Resize with the exact UBound values.
  • Using a 1D array with a multi-cell range. Direct assignment fails or writes only the first element. Use Transpose or a loop.
  • Forgetting that array bounds may start at 0. If you declare Dim arr(3), the default lower bound is 0 (unless Option Base 1 is set), so UBound is 3 but there are 4 elements. Use explicit bounds like 1 To 3 to avoid confusion.
  • Assigning to a range on the wrong sheet. Range("A1") refers to the active sheet. If your macro activates another sheet, the data lands in the wrong place. Qualify with a worksheet object: Worksheets("Sheet1").Range("A1").
  • Not clearing the destination. Old data outside the new array's size remains, giving a misleading result.

Tips

  • Use Variant arrays for reading from and writing to ranges; they handle mixed data types and are required for direct range assignment.
  • For very large arrays, direct assignment is dramatically faster than looping. A loop writing 10,000 cells can take seconds; a single assignment is near-instant.
  • If you need to write to a specific column or row, build a 2D array with one dimension of size 1, or use Application.Transpose carefully.
  • Use With blocks to avoid repeating worksheet references and reduce errors.
  • Test with a small array first, then scale up. Check the Immediate Window (Ctrl+G) with Debug.Print to inspect array bounds.

Troubleshooting

  • Run-time error 13: Type mismatch. Usually caused by assigning a 1D array to a multi-cell range, or by an array that is not Variant. Convert with Transpose or declare as Variant.
  • Run-time error 9: Subscript out of range. The array bounds do not match the target range, or the worksheet name is wrong. Check UBound values and sheet names.
  • Only the first value appears. You likely assigned a 1D array directly. Use Transpose or a loop.
  • Data appears in the wrong cells. The active sheet changed, or the anchor cell is not what you expected. Qualify the range with a worksheet object.
  • Macro runs but nothing happens. Check that the subroutine is actually called (e.g., you pressed F5 in the right procedure) and that the target sheet is not protected. Unprotect it or write to an unprotected sheet.
  • Transpose fails on large arrays. It has a size limit. For large 1D arrays, use a loop or reshape into a 2D array manually.

Frequently Asked Questions

Can I write a 1D array directly to a row or column?

Not directly. You must either loop through the elements or use Application.Transpose to convert it to a 2D array first. Transpose works for arrays up to about 65,536 elements.

Why does my array write only one value?

This typically happens when you assign a 1D array to a multi-cell range without transposing, or when the array is not a Variant. Ensure the array is 2D and Variant, and that the target range is sized with Resize.

How do I write an array starting at a specific cell, like B5?

Use Range("B5").Resize(UBound(arr,1), UBound(arr,2)).Value = arr. The array fills from B5 down and to the right.

Does the array need to be Variant?

Yes, for direct assignment to a range, the array should be declared as Variant. Other types may cause a type mismatch error.

Can I write to a different worksheet?

Yes, qualify the range with the worksheet object: Worksheets("Sheet2").Range("A1").Resize(...).Value = arr. Make sure the sheet exists and is not protected.

What if my array has more than two dimensions?

Excel ranges are two-dimensional. You cannot directly assign a 3D array. You would need to loop and write slices, or flatten the data into a 2D array first.