Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 我如何建立一个;“锯齿阵列”;在VBA中?_Arrays_Vba_Jagged Arrays - Fatal编程技术网

Arrays 我如何建立一个;“锯齿阵列”;在VBA中?

Arrays 我如何建立一个;“锯齿阵列”;在VBA中?,arrays,vba,jagged-arrays,Arrays,Vba,Jagged Arrays,我有一个满是孩子的教室,每个孩子都必须列出他们最喜欢的玩具作为作业。有些孩子只列出一个玩具,而有些孩子列出更多 我如何创建一个锯齿状数组,使孩子们(x)(y)…其中x是我们班上孩子的数量,y是他们最喜欢的玩具列表?您可以使用集合集合 Public Sub Test() Dim list As New Collection Dim i As Integer, j As Integer Dim item As Collection For i = 1 To 10

我有一个满是孩子的教室,每个孩子都必须列出他们最喜欢的玩具作为作业。有些孩子只列出一个玩具,而有些孩子列出更多


我如何创建一个锯齿状数组,使孩子们(x)(y)…其中x是我们班上孩子的数量,y是他们最喜欢的玩具列表?

您可以使用集合集合

Public Sub Test()

    Dim list As New Collection
    Dim i As Integer, j As Integer
    Dim item As Collection
    For i = 1 To 10
        Set item = New Collection
        For j = 1 To i
            item.Add "Kid" & CStr(i) & "Toy" & CStr(j)
        Next j
        list.Add item
    Next i

    Debug.Print "Kid 4, Toy 2 = " & list(4)(2)
End Sub
输出孩子4,玩具2=Kid4Toy2的“锯齿数组”是数组数组的俚语。VBA的
变量
数据类型可以包含几乎任何内容*,包括数组。因此,您可以创建一个类型为
Variant
的数组,并为其每个元素指定一个任意长度的数组(即并非所有元素都必须具有相同的长度)

下面是一个例子:

Dim nStudents As Long
Dim iStudent As Long
Dim toys() As Variant
Dim nToys As Long
Dim thisStudentsToys() As Variant

nStudents = 5 ' or whatever

ReDim toys(1 To nStudents) ' this will be your jagged array

For iStudent = 1 To nStudents
    'give a random number of toys to this student (e.g. up to 10)
    nToys = Int((10 * Rnd) + 1)
    ReDim thisStudentsToys(1 To nToys)

    'code goes here to fill thisStudentsToys()
    'with their actual toys

    toys(iStudent) = thisStudentsToys
Next iStudent

' toys array is now jagged.

' To get student #3's toy #7:
MsgBox toys(3)(7)
'will throw an error if student #3 has less than 7 toys

*一个值得注意的例外是用户定义的类型。变体不能包含这些内容。

您还可以将玩具列表连接成一个管道分隔字符串,然后在需要时使用拆分将字符串转换为数组:

Sub UntangleTheString()

Dim sToys As String
Dim aToys() As String
Dim x As Long

sToys = "baseball|doll|yoyo"

aToys = Split(sToys, "|")

For x = LBound(aToys) To UBound(aToys)
    Debug.Print aToys(x)
Next

End Sub

Jean Francois指出,每个元素都可以是一个长度不等的数组。我要补充的是,每个元素也可以是其他类型,而不必是数组。例如:

Dim c as New Collection
Dim a(1 to 5) as Variant

c.Add "a","a"
c.Add "b","b"
a(1) = 5
a(2) = Array(2,3,4)
set a(3) = c
a(4) = "abcd"
a(5) = Range("A1:A4").Value
然后可以根据每个元素的隐式类型引用各个子元素:

a(2)(1)=3

a(3)(1)=“a”


a(5)(2,1)=单元格A2中的任何内容。

您不一定需要锯齿状数组来处理场景,因为2D数组(r,c)也可以工作。每个孩子一行,每个孩子一列。数组维度将为(#个孩子,最多#个礼物),这意味着一些插槽将为空或0(取决于您的数据类型)。但至少通过这种方式,您不必在每次为孩子添加礼物时重新分配阵列。

我不熟悉的集合集合与阵列阵列或锯齿阵列有何不同?您可以从集合中添加和删除项目,但不能从阵列中添加和删除项目。如果需要,还可以使用索引之外的键访问项目。可能您使用孩子们的姓名或ID作为集合的键。请参阅我的答案,其中解释了锯齿阵列。当您将
ThistudentsToys
重新分配时,是否创建了新的内存位置?我以为VBA中的数组是引用。我只想再次检查:在for循环的第二次迭代中,重写
thistudentstoys
数组不会改变
toys
的第一个索引?如果我没有说清楚,请告诉我。非常感谢。