无法对数组进行切片
Posted
技术标签:
【中文标题】无法对数组进行切片【英文标题】:Unable to slice an array 【发布时间】:2022-01-18 05:12:16 【问题描述】:我正在尝试从不连续的行中获取数据。列是固定的,但行号会有所不同。我期待的结果是一个二维数组。我不知道我做错了什么,但是使用索引功能的切片不起作用。
'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。是否有可能通过切片二维数组来获得二维数组?如果是,请指出正确的方向。
【问题讨论】:
【参考方案1】:行数组需要垂直:
arr = Application.Index(Sheet2.Range("A:F"), Application.Transpose(Array(100, 500, 900)), Array(1, 2, 3, 4, 5, 6))
人们总是可以创建一个从 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)
【讨论】:
不错。那效果很好。但是第三个参数总是需要的吗?我的意思是我想要来自所有列的数据。在示例中,我给出了 A:F,但如果我的范围跨越多个列,如 AE:DE,该怎么办。是否可以缩短第三个参数? @saikrishna 不,但你可以用循环中的数字填充一维变体数组,然后使用它。【参考方案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))
快速修复
从二维基于一的数组中的二维基于一的数组返回三行六列。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
【讨论】:
非常感谢您提供非常详细的回答。我希望我能接受多种解决方案作为答案。以上是关于无法对数组进行切片的主要内容,如果未能解决你的问题,请参考以下文章