Question posted 2013 · +2 upvotes
I have text data in Excel worksheet in the cells B6:H14.
Some rows will have 2 cells with contents while others have 4 and some will have 7. How do I copy these to a 2 dimensional array? I know the dimensions already and so, I am ok with the dimensions not being declared dynamic code.
Do I need to use a loop (which I am currently planning to use)?
Or is there an easier / more elegant way?
Accepted answer +34 upvotes
Assuming your spreadsheet looks kind of like this

There is a really easy way to stick that in a 2D array
Dim arr as Variant
arr = Range("B6:H14").Value
The easiest way to print this array back to spreadsheet
Sub PrintVariantArr()
Dim arr As Variant
arr = Range("B6:H14")
Range("B16").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
End Sub
Or you can iterate/loop the array
Sub RangeToArray()
Dim arr As Variant
arr = Range("B6:H14").Value
Dim r As Long, c As Long
r = 16
c = 2
Dim i, j
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
Cells(r, c) = arr(i, j)
c = c + 1
Next j
c = 2
r = r + 1
Next i
End Sub
And your array printed back to the spreadsheet

3 code variants in this answer
- Variant 1 — 2 lines, starts with
Dim arr as Variant - Variant 2 — 8 lines, starts with
Sub PrintVariantArr() - Variant 3 — 20 lines, starts with
Sub RangeToArray()
Top excel-vba Q&A (6)
- How to clear the entire array? +58 (2010)
- How to change Format of a Cell to Text using VBA +55 (2011)
- Download attachment from Outlook and Open in Excel +43 (2012)
- Can a VBA function in Excel return a range? +36 (2009)
- Hiding an Excel worksheet with VBA +33 (2009)
- How do I slice an array in Excel VBA? +31 (2008)
excel-vba solutions on this site
— top 4%.