无法对阵列进行切片

Unable to slice an array

提问人:sai krishna 提问时间:12/15/2021 最后编辑:braXsai krishna 更新时间:12/15/2021 访问量:116

问:

我正在尝试从不连续的行中获取数据。列是固定的,但行号会有所不同。我期待的结果是一个二维数组。我不知道我做错了什么,但是使用索引功能的切片不起作用。

'Just for example to get the data from row number 100, 500 and 900 and the columns from A to F
arr = Application.Index(Sheet2.Range("A:F"), array(100, 500, 900))

我想如果第三个参数留空,就会完成行切片。但生成的数组是一维的(大小为 3),并且填充了错误 2023。

所以我也通过给出第三个参数再次尝试。

arr = Application.Index(Sheet2.Range("A:F"), array(100, 500, 900), array(1, 2, 3, 4, 5, 6))

即使是现在,生成的数组也是一维的(大小为 6),但前三个索引有数据,其余索引有错误 2042。是否有可能通过切片二维数组来获得二维数组?如果是,请为我指出正确的方向。

数组 Excel VBA 多维数组 切片

评论


答:

1赞 Scott Craner 12/15/2021 #1

行数组必须是垂直的:

arr = Application.Index(Sheet2.Range("A:F"), Application.Transpose(Array(100, 500, 900)), Array(1, 2, 3, 4, 5, 6))

enter image description here

人们总是可以创建一个从 1 到列数的一维数字数组,并使用它:

Dim rng As Range
Set rng = Sheet2.Range("A:F")

Dim test As Variant
ReDim test(rng.Columns.Count - 1)

Dim i As Long
For i = LBound(test) To UBound(test)
    test(i) = i + 1
Next i

Dim arr As Variant
arr = Application.Index(rng, Application.Transpose(Array(100, 500, 900)), test)

评论

0赞 sai krishna 12/15/2021
好。效果很好。但是第三个参数总是必需的吗?我的意思是我想要所有列的数据。在示例中,我给出了 A:F,但是如果我的范围跨越多个列,例如 AE:DE,该怎么办?是否可以缩短第 3 个参数?
0赞 Scott Craner 12/15/2021
@saikrishna没有,但你可以用循环中的数字填充一个一维变体数组,然后使用它。
1赞 VBasic2008 12/15/2021 #2

切片数组行

  • 请注意,对于连续列,您可以使用:

    [Row(1:1)] ' first column
    [Row(1:3)] ' first three columns
    [Row(2:5)] ' four columns after the first
    [Row(3:4)] ' two columns after the second
    
  • 如果您还需要灵活地选择不连续的列,那么您可以使用以下方法代替:[Row(1:6)]

    Application.Transpose([{1,3,4,6}])
    Application.Transpose(Array(1, 3, 4, 6))
    

快速修复

  • 从 2D 1 个数组中的 2D 1 个数组返回 3 行乘以 6 列。
Sub QuickFix()
    ' Don't use entire columns, it takes too long.
    Dim rg As Range: Set rg = Sheet2.Range("A1").CurrentRegion.Columns("A:F")
    'Debug.Print rg.Address(0, 0)
    Dim arr As Variant
    arr = Application.Transpose(Application.Index(rg.Value, [{100,500,900}], [Row(1:6)]))
    ' Three rows, six columns ('H1:M3')
    Sheet2.Range("H1").Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
End Sub

一项研究

Sub Random()
    Dim arr() As Variant
    
    ' A Row (1D one-based: one row, three columns)
    arr = [{3,5,7}]
    Debug.Print "Row (Random)"
    Debug.Print LBound(arr), UBound(arr)
    
    ' A Column (2D one-based: three rows, one column)
    arr = Application.Transpose([{3,5,7}])
    Debug.Print "Column (Random)"
    Debug.Print LBound(arr, 1), UBound(arr, 1), LBound(arr, 2), UBound(arr, 2)

End Sub

Sub Sequence()
    Dim arr() As Variant
    
    ' A Row (1D one-based: one row, six columns)
    arr = Application.Transpose([(Row(1:6))])
    Debug.Print "Column (Sequence)"
    Debug.Print LBound(arr, 1), UBound(arr, 1)

    ' A Column (2D one-based: six rows, one column)
    arr = [Row(1:6)]
    Debug.Print "Row (Sequence)"
    Debug.Print LBound(arr, 1), UBound(arr), LBound(arr, 2), UBound(arr, 2)

End Sub

Sub TwoD()
    
    ' Source Array (2D one-based: ten rows, six columns)
    Dim sData As Variant: sData = Range("A1:F10")
    Debug.Print "Source"
    Debug.Print LBound(sData, 1), UBound(sData, 1), LBound(sData, 2), UBound(sData, 2)
    
    ' Transposed Array (2D one-based: six rows, three columns)
    Dim tData As Variant
    tData = Application.Index(sData, [{3,5,7}], [Row(1:6)])
    Debug.Print "Transposed (Wrong)"
    Debug.Print LBound(tData, 1), UBound(tData, 1), LBound(tData, 2), UBound(tData, 2)
    
    ' Destination Array (2D one-based: three row, six columns)
    Dim dData As Variant
    dData = Application.Transpose(Application.Index(sData, [{3,5,7}], [Row(1:6)]))
    Debug.Print "Destination (Correct)"
    Debug.Print LBound(dData, 1), UBound(dData, 1), LBound(dData, 2), UBound(dData, 2)

End Sub

评论

0赞 sai krishna 12/15/2021
非常感谢您提供非常详细的答案。我希望我能接受多种解决方案作为答案。