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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Assuming the variable Arr is declared as a Variant and that you are loading a table named "Table1" located on Sheet1 into it, then this should do it...
VBA Code:
Arr = Sheets("Sheet1").ListObjects("Table1").DataBodyRange
 
Upvote 0
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
 
Upvote 0
Solution
Assuming the variable Arr is declared as a Variant and that you are loading a table named "Table1" located on Sheet1 into it, then this should do it...
VBA Code:
Arr = Sheets("Sheet1").ListObjects("Table1").DataBodyRange
If data is coming from the calling sheet, can I eliminate the Sheet("Sheet1")? That way I can call it from other sheets in the same workbook.
 
Upvote 0
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
Amazing. I thought I tried that, but I must have done something wrong.

Thank you.

PS: Yes, it's just a named contiguous range, not an actual table. I've got to be more careful with the terminology.
 
Upvote 0
I thought maybe my error was that I declared Arr as a dynamic array.

VBA Code:
Dim Arr()

But I just tried it and it works that way as well. Is there any difference depending on whether I declare it as an array or not?
 
Upvote 0
PS: On a (marginally) related question, is there a way to import a row of text values into a space delimited scalar variable?

Suppose I have assigned the name "Initials" to the range (C5:G5) containing the values "BC, JC, WC, BS, & PS". Can I import those values into a variable as a space delimited string ("BC JC WC BS PS") with a single VBA statement?
 
Upvote 0

Forum statistics

Threads
1,223,718
Messages
6,174,082
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