如果我有5个具有相同维度的数组,我可以将它们组合成一个包含5个子阵列的数组。
va = ws1.Range("A2", ws1.Cells(Rows.Count, "A").End(xlUp)).Value
vb = ws1.Range("D2", ws1.Cells(Rows.Count, "D").End(xlUp)).Value
vc = ws1.Range("F2", ws1.Cells(Rows.Count, "F").End(xlUp)).Value
vd = ws1.Range("C2", ws1.Cells(Rows.Count, "C").End(xlUp)).Value
ve = ws1.Range("E2", ws1.Cells(Rows.Count, "E").End(xlUp)).Value
我可以这样做:
ReDim vx(1 To UBound(va, 1), 1 To 5, 1 To 1)
vx(1,1,1) = va
vx(1,2,1) = vb
vx(1,3,1) = vc
vx(1,4,1) = vd
vx(1,5,1) = ve
或者我是否必须逐个进行?
答案 0 :(得分:2)
如果元素的数量不相同,则将数组组合到数组中通常称为锯齿状数组(info for C# jagged arrays和How do I set up a "jagged array" in VBA?以及Can you declare jagged arrays in excel VBA directly?)。在以下情况下,va
有一个元素超过vb
和vc
:
Option Explicit
Public Sub TestMe()
Dim va, vb, vc, vx
va = Array(1, 2, 3, 4)
vb = Array(11, 22, 33)
vc = Array(111, 222, 333)
ReDim vx(3)
vx(0) = va
vx(1) = vb
vx(2) = vc
Debug.Print vx(0)(1)
Debug.Print vx(0)(2)
Debug.Print vx(1)(1)
Debug.Print vx(2)(2)
End Sub
您只需声明vx
即可按原样分配va
,vb
,vc
。然后你可以访问这样的元素 - > vx(1)(0)