Best way of putting various columns from a Table into an array

RawlinsCross

Active Member
Joined
Sep 9, 2016
Messages
437
Good day,

I have an excel table with 15 columns and I want to place 7 of these columns (non-contiguous) in a 2-D variant array. What are my best options here? I have a thought of making a named range with the various columns but this seems like I'm ignoring the ListColumns property inherent in tables? Thoughts?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Say your table is named "Table1". Then you could put the entire table's data into a 2-D variant array and just work with the specific columns of interest. Maybe something like this:
VBA Code:
Sub Test()
Dim R As Range, V As Variant, LO As ListObject
Set LO = ActiveSheet.ListObjects("Table1")
Set R = LO.DataBodyRange
V = R.Value
'rest of code
'example of values in first data row of columns 1,3, and 5 of the table
MsgBox V(1, 1) & vbTab & V(1, 3) & vbTab & V(1, 5)
End Sub
 
Upvote 0
Hi JoeMo,

The issue is that I want to put the resulting array in a ListBox on a userform with a ColumnCount of 7. So I want to take from Table1 columns 2, 4, 6, 7, 8, 9, 11 - and put them in the variant array. Then do some adjustments on the array (we don't need to worry about that here) and then put the array into the listbox in its entirety.
 
Upvote 0
How about
VBA Code:
Sub RawlinsCross()
   Dim Ary As Variant
  
   Ary = ActiveSheet.ListObjects(1).DataBodyRange.Value2
   Ary = Application.Index(Ary, Evaluate("row(1:" & UBound(Ary) & ")"), Array(2, 4, 6, 7, 8, 9, 11))
End Sub
 
Upvote 0
How interesting is that (and what an honor to have a subroutine named after me! hahaha). The "evaluation" keyword in particular is very neat. Looking up on it now.

I'm thinking I can get two birds stoned at once here! (Canadian TV show joke). I can do the adjustments I hinted in my first message to flag the rows I want to keep and put them in that first argument as another array. Very cool! (I think)
 
Upvote 0
No? So I can't do something like this?

VBA Code:
Ary = Application.Index(Ary, Evaluate(Array(1, 3, 4, 5, 6, 7, 9, 11, 45, 56), Array(2, 4, 6, 7, 8, 9, 11))

The first array would be formed by an evaluation subroutine that would select row numbers?
 
Last edited:
Upvote 0
Well, I was going to put in some subroutine that scanned the array to make another raw of filtered rows and pass that onto the first argument of Evaluate but I thought it too complex relative to the alternative. So the selected columns of the table that form the array go into a userform multi-column listbox so I ended up doing the following:
1. Dump the entire table into the array
2. Select those columns that will be in the listbox (via the Index/Evaluate code Fluff mentioned above)
3. Insert the array into the listbox
4. Run the filter on the listbox entries

VBA Code:
Set mwSht = ThisWorkbook.Worksheets("RawData")
vTableArray = mwSht.ListObjects("Table1").DataBodyRange.Value
'Only include the relevant columns
vTableArray = Application.Index(vTableArray, Evaluate("row(1:" & UBound(vTableArray) & ")"), Array(2, 4, 6, 7, 8, 9, 11))

Me.lbHistory.List = vTableArray

Call CleanUpListBox

Clean Up ListBox
VBA Code:
Private Sub CleanUpListBox()

Dim i As Long

With Me.lbHistory
For i = .ListCount - 1 To 0 Step -1
     If .List(i, 2) = "-" Then
        .RemoveItem (i)
    Else
        'perform some minor formatting on remaining rows in certain columns
        .List(i, 0) = Format(.List(i, 0), "dd-mmm-yy h:mm AM/PM") 'Format Date
        .List(i, 1) = Format(.List(i, 1), "0.00") 'Format duration in hours
    End If
Next i
End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,958
Latest member
Hat4Life

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