简体   繁体   中英

Get columns from array VBA

I'm having difficulty returning a set of columns from an array. I've returned an sql select statement to my variant array. It has 16 columns. I need to grab 12 of the 16 columns to return to my spreadsheet from a specific row in the array. I'm using this code to get my row:

    If UBound(Filter(budget, Cells(i, 1).Value, , vbBinaryCompare)) >= 0 And Cells(i, 1).Value <> "" Then

How do I then get just the 12 columns I need? The columns are the final 12 columns in the array and will always be in the order I need them.

Thanks in advance for the help!

That's going to depend on the shape of the array.
If it's a one dimension array

 a = Filter(Selection, Cells(i, 1).Value, , vbTextCompare)
 rows = UBound(a)
 Cells(i, 1).Resize(1, rows ) = Application.WorksheetFunction.Transpose(a)

Notice that WorksheetFunction.Transpose(a) swaps the rows and columns of the array.

Multi-dimensional arrays will depend on how they where created.

We can think of it like this

Database Query Arrays: a = recordset.getRows()

And

Dim a(10, 100) Redim Preserve a(10, Ubound(a) + 1)

Are aligned like this a(columns, rows) because you can only re-size the last dimentsion of an array.
So we would: a = Filter(Selection, Cells(i, 1).Value, , vbTextCompare) rows = UBound(a, 2) columns = UBound(a, 1) Cells(i, 1).Resize(columns, rows ) = Application.WorksheetFunction.Transpose(a)

Excel Range Arrays are base 1 and have the same shape as the Range itself.

a = Range("A1:K200").Value
a(1,1) = cells(1, 1) evaluates to True

So you can do this

a = Range("A1:K200").Value
Range("A1:K200") = a

Or a = Filter(Selection, Cells(i, 1).Value, , vbTextCompare) rows = UBound(a, 1) columns = UBound(a, 2) Cells(i, 1).Resize(rows, columns) = a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM