Transfer table to vba array

KerryFSA

New Member
Joined
Dec 5, 2018
Messages
49
I have a simple table on worksheet "Table" in Excel. Let's say the table is called "SortTable" and has 8 rows by 4 columns. I want to populate a vba array with the contents of the table. I have seen 3 or 4 ways of doing this. None work for me - I get the error message "Can't assign to array". Here are a couple of examples of how I attempted this. Would appreciate your help. I use "Option Base 1". Thanks very much.

Dim SArray(1 To 8, 1 To 4) As Integer, SortTable As ListObject
SArray = Worksheets("Table").ListObjects("SortTable")


OR
SArray = Worksheets("Table").Range("A1:D8")
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Declare sArray as variant & don’t set the dimension.
And since “SortTable” is the table’s name then you don’t need to declare it.
Like this:
Code:
Dim SArray As Variant
SArray = Worksheets("Table").ListObjects("SortTable").Range
If you want only the data part (without the header):
Code:
SArray = Worksheets("Table").ListObjects("SortTable").DataBodyRange
 
Upvote 0
OK, that seems to get the table transferred.
Now, I write the following to pick out a value from the newly created array:

x = SArray(2,1)

and get the error "Expected array."

What's wrong?
 
Upvote 0
That should work, I tried it & it worked.
So I don't know what cause the error.
Can you post the whole code?
 
Upvote 0
I changed SArray to variant. Seems to do the trick.
Won't know for sure until I finish the macro.
Thanks again for your help.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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