Simple way to load a worksheet table into a VBA array?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
I need to import a worksheet table into a VBA array. I found several solutions, but they all seemed unnecessarily complicated. One even involved about 20 lines of code.

Isn't there a simple expression do do this? I have assigned a name in the worksheet to the table.

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If it's not an actual Excel table, you just assigned a name to a (contiguous) range of cells:

VBA Code:
Arr = Range("MyData").Value
Does it matter whether I declare Arr as an array?
VBA Code:
Dim Arr()
Arr = Range("MyData").Value
Or as a scalar?
VBA Code:
Dim Arr
Arr = Range("MyData").Value
It seems to work both ways.
 
Upvote 0
One more question. It appears that data read in this way creates a 2-dimensional array even if the data is a single row or column. Is that correct? Or did I do something wrong?

These statements are in the module outside any code:
VBA Code:
Const nrAlbumNums As String = "AlbumNums"
Private AlbumNums() As Variant

This code uses these variables:
VBA Code:
AlbumNums = Range(nrAlbumNums).Value

For row = LBound(AlbumNums) To UBound(AlbumNums)
  temp = AlbumNums(row, 1)
      . . .
Next row

I first tried it with a single subscript, but got an error.

I'm OK with this. It probably makes sense. Just curious.

Thanks
 
Upvote 0
VB will automatically create a two-dimensional array from any multi-cell range assigned to (I always use) a Variant variable. You can force it to return one-dimensional arrays from single rows or single columns like this...

SingleRowArray = Application.Index(Range("C5:X5").Value, 1, 0)

SingleColumnArray = Application.Transpose(Range("C5:C26").Value)

Note: These arrays will all start with index 1 (not 0).
 
Upvote 0
VB will automatically create a two-dimensional array from any multi-cell range assigned to (I always use) a Variant variable. You can force it to return one-dimensional arrays from single rows or single columns like this...

SingleRowArray = Application.Index(Range("C5:X5").Value, 1, 0)

SingleColumnArray = Application.Transpose(Range("C5:C26").Value)

Note: These arrays will all start with index 1 (not 0).
Thanks
 
Upvote 0

Forum statistics

Threads
1,223,721
Messages
6,174,091
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top