Creating array of selected columns from named table

Westbury

Board Regular
Joined
Jun 7, 2009
Messages
139
My fundamental question is how do I put columns 1, 7 & 8 from a named table into an array?

I've studied a similar question in Best way of putting various columns from a Table into an array and particularly the solution identified by Fluff
but keep getting a runtime error 9 subscript out of range.

My named table definitely exists but it appears that the vba doesn't recognise it. I'm using Excel 2013 if that is of significance.

This is the code so far..

VBA Code:
Sub ColumnsTable_To_Array()

Dim myTable As ListObject
Dim myArray As Variant

'Set path for Table variable
myArray = Worksheets("Data list").ListObjects("Data").Range

Erase myArray
End Sub

Thanks
Geoff
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Glad you sorted it & thanks for the feedback.
 
Upvote 0
Similar to the link you referred to, but structured differently...
VBA Code:
Sub Westbury()
  Dim Arr As Variant
  With Worksheets("Data list").ListObjects("Data").DataBodyRange
    Arr = Application.Index(.Value, Evaluate("ROW(1:" & .Rows.Count & ")"), [{1,7,8}])
  End With
  '
  '  Arr is an array containing all the rows of data from the referenced
  '  table drawn from the 1st, 7th and 8th columns of that table
  '
End Sub
 
Upvote 0
It would be useful to be able to copy the first row of the data table with the column identities. Given that this is now a named table is there any special command to do this ?
 
Upvote 0
Just change the DataBodyRange property of the ListObjects object to Range.
 
Upvote 0
It would be useful to be able to copy the first row of the data table with the column identities. Given that this is now a named table is there any special command to do this ?
but with the same selection of cells, cols 1,7 and 8
 
Upvote 0
Yes, what I suggested will do that. Here is the code with the modification I mentioned...
VBA Code:
Sub Westbury()
  Dim Arr As Variant
  With Worksheets("Data list").ListObjects("Data").Range
    Arr = Application.Index(.Value, Evaluate("ROW(1:" & .Rows.Count & ")"), [{1,7,8}])
  End With
  '
  '  Arr is an array containing all the rows of data from the referenced
  '  table drawn from the 1st, 7th and 8th columns of that table
  '
End Sub
 
Upvote 0
Rick,

thanks for your reply. This last code produces an array of the table data including the headers. I'm now looking for an array just to capture the headers, but agian only columns 1,7,8

You posted VBA - Collecting all the heading names into an array in 2017. The code
VBA Code:
Headers = Application.Index(Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Value, 1, 0)[CODE=vba]

gives me all of the headers. How do I convert this to refer to my named table and only to pick up cols 1,7 & 8 ?

Thanks.
 
Upvote 0
Try it this way...
VBA Code:
Sub GetCertainHeaders()
  Arr = Application.Index(Worksheets("Data list").ListObjects("Data").HeaderRowRange,1,[{1,7,8}])
  '
  '  Arr is an array containing the values from Columns 1,7,8 of the table's header row
  '
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,569
Messages
6,120,286
Members
448,953
Latest member
Dutchie_1

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