简体   繁体   English

从数组VBA获取列

[英]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. 我向我的变量数组返回了一条sql select语句。 It has 16 columns. 它有16列。 I need to grab 12 of the 16 columns to return to my spreadsheet from a specific row in the array. 我需要获取16列中的12列,才能从数组中的特定行返回到电子表格。 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? 然后如何只获取所需的12列? The columns are the final 12 columns in the array and will always be in the order I need them. 这些列是数组中的最后12列,它们将始终按照我需要的顺序排列。

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. 请注意,WorksheetFunction.Transpose(a)交换数组的行和列。

Multi-dimensional arrays will depend on how they where created. 多维数组将取决于它们在何处创建。

We can think of it like this 我们可以这样想

Database Query Arrays: a = recordset.getRows() 数据库查询数组:a = recordset.getRows()

And

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

Are aligned like this a(columns, rows) because you can only re-size the last dimentsion of an array. 之所以这样对齐是因为a(列,行),因为您只能调整数组的最后维度的大小。
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) 因此我们将: a = Filter(Selection,Cells(i,1).Value,vbTextCompare)行= UBound(a,2)列= UBound(a,1)Cells(i,1).Resize(列,行)= Application.WorksheetFunction.Transpose(a)

Excel Range Arrays are base 1 and have the same shape as the Range itself. Excel范围数组以1为底,并且具有与范围本身相同的形状。

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 或a =筛选器(选择,单元格(i,1)。值,vbTextCompare)行= UBound(a,1)列= UBound(a,2)单元格(i,1)。调整大小(行,列)= a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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