从数组 VBA 中获取列

Get columns from array VBA

我在 return 从数组中提取一组列时遇到困难。我已经 return 对我的变量数组编辑了 sql select 语句。它有 16 列。我需要从数组中的特定行中获取 16 列中的 12 列 return 到我的电子表格。我正在使用此代码获取我的行:

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

我如何才能只获得我需要的 12 列?这些列是数组中的最后 12 列,并且始终按照我需要的顺序排列。

在此先感谢您的帮助!

这取决于数组的形状。
如果是一维数组

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

注意 WorksheetFunction.Transpose(a) 交换数组的行和列。

多维数组将取决于它们的创建方式。

我们可以这样想

Database Query Arrays: a = recordset.getRows()

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

像这样对齐 a(columns, rows) 因为你只能调整数组最后一维的大小。
所以我们会: a = Filter(Selection, Cells(i, 1).Value, , vbTextCompare) 行 = UBound(a, 2) 列 = UBound(a, 1) 单元格(i,1)。调整大小(列,行)=应用程序。WorksheetFunction.Transpose(a)

Excel 范围数组以 1 为底并且与范围本身具有相同的形状。

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

所以你可以这样做

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

或者 a = Filter(Selection, Cells(i, 1).Value, , vbTextCompare) 行 = UBound(a, 1) 列 = UBound(a, 2) 单元格(i,1)。调整大小(行,列)= a