Arrays 在VBA、Excel中自动创建不同的、可识别的集合

Arrays 在VBA、Excel中自动创建不同的、可识别的集合,arrays,vba,excel,collections,Arrays,Vba,Excel,Collections,所以我有大量的玩具卡车,我把它们储存在一个集合中(因为一开始我不知道会有多少辆卡车),每辆卡车都有很多属性,比如销售量、价格、轴距等。这些卡车的一个特定属性是重量限制,即这个玩具可以承载多少重量,只有一定数量的不同重量限制,但我不知道启动宏时会有多少 我想做的是循环整个集合,并将卡车添加到不同的集合中,具体取决于它们的重量,但如上所述,我不知道会有多少不同的重量限制 因此,我想我的问题是,我如何能够自动创建易于识别的集合,当发现一辆具有新重量限制的卡车在所有卡车中循环时,会创建每个集合 举个例子

所以我有大量的玩具卡车,我把它们储存在一个集合中(因为一开始我不知道会有多少辆卡车),每辆卡车都有很多属性,比如销售量、价格、轴距等。这些卡车的一个特定属性是重量限制,即这个玩具可以承载多少重量,只有一定数量的不同重量限制,但我不知道启动宏时会有多少

我想做的是循环整个集合,并将卡车添加到不同的集合中,具体取决于它们的重量,但如上所述,我不知道会有多少不同的重量限制

因此,我想我的问题是,我如何能够自动创建易于识别的集合,当发现一辆具有新重量限制的卡车在所有卡车中循环时,会创建每个集合

举个例子,如果我有10辆卡车,其中6辆可以承载2kg,4辆可以承载3kg,我想要两个系列,将2kg和3kg卡车分开

我曾考虑过创建一个三维数组,但这将涉及大量的“空白空间”,其中一些重量限制比其他限制更常见,因此需要更多的代码来处理,这并不理想。至于创建锯齿状数组,我遇到了同样的问题,即不知道如何自动创建不同的、易于识别的数组

理想情况下,我想做的是动态创建一个二维数组,其中第一行是权重限制,第二行是对集合的引用(在向数组添加新权重限制时自动创建),但我认为这是不可能的。。。(由于ReDim保留而包含权重限制的行)

以下是我目前正在做的,但显然并不理想: (此外,重量限制不会每次增加2)


下面是一个函数,它将卡车集合作为参数,并返回包含不同重量限制的尽可能多的子集合的集合。每个此类子集合包含具有此特定重量限制的所有卡车

Public Function divideIntoCollections(trucks As Collection) As Collection
    Dim objTruck As Truck
    Dim colWeightGroup As Collection
    Dim weightLimit As Double
    '----------------------------------------------------------------


    Set divideIntoCollections = New Collection


    For Each objTruck In trucks

        weightLimit = objTruck.weightLimit

        'Try to find a subcollection having the same key as the 
        'weight limit of the current truck.
        On Error Resume Next
        Set colWeightGroup = divideIntoCollections.Item(CStr(weightLimit))
        On Error GoTo 0

        'If such subgroup has not been found, it means
        'this is the first truck with such weight limit.
        'We need to create a new subcollection for trucks
        'with such weight limit and add it to the result collection.
        If colWeightGroup Is Nothing Then
            Set colWeightGroup = New Collection
            Call divideIntoCollections.Add(Item:=colWeightGroup, Key:=CStr(weightLimit))
        End If


        'Add current truck to the proper subcollection.
        Call colWeightGroup.Add(Item:=objTruck)


    Next objTruck


End Function
如何使用它的一些示例:

Sub Main()
    Dim trucks As Collection
    Dim trucksByWeight As Collection
    '--------------------------------------------------

    Set trucks = LoadCollection '<-- your own method to initially
                                '    load trucks into collection.

    Set trucksByWeight = divideIntoCollections(trucks)


    'Here is how you can get access to the trucks with the given weight.
    Dim trucks12 As Collection
    Set trucks12 = trucksByWeight.item("12")


    'Here is how you can print all weight limits and the number of trucks
    'appended to this group.
    Dim subCol As Object
    For Each subCol In trucksByWeight
        Debug.Print " Weight limit: " & subCol.item(1).weightLimit & _
                    " Trucks: " & subCol.Count
    Next item


End Sub
Sub-Main()
暗淡的卡车作为收集
Dim卡车按重量作为集合
'--------------------------------------------------

设置trucks=LoadCollection'以下是如何用重量限制和集合填充数组:

Sub FillArray()

Dim arr() As Variant
Dim col123 As Collection

Set col123 = New Collection
col123.Add 1, "FirstKey"
col123.Add "Whatever", "Foo"

ReDim arr(1 To 2, 1 To 1) As Variant
arr(1, 1) = 1000
Set arr(2, 1) = col123

ReDim Preserve arr(1 To 2, 1 To 2) As Variant
Set col123 = New Collection
col123.Add "Something", "InSomeKey"
col123.Add "Another thing", "In another key"
arr(1, 2) = 2000
Set arr(2, 2) = col123

End Sub

我认为你关于二维数组的想法是可行的,但是第一个维度应该包含集合,第二个维度应该包含重量限制。这样,Redim Preserve就不会引起问题。但是您可以将集合分配给数组元素吗?当我尝试(下面的代码)时,我得到了“运行时错误”450:参数数量错误或属性分配无效。“Dim coll123作为集合:Set coll123=新集合:Dim arr(1,4):arr(1,1)=coll123@JamesChristen您可以这样做,但集合是一个对象,所以您需要使用
Set
关键字:
Set-arr(1,1)=coll123
@mielk-Oh,是的,当然!谢谢你的回答,当我这样试的时候,忘记了
Set
。填充完数组后访问集合的语法是什么?没关系,我知道了:
Debug.Print WL(i,2)(j).WeightLimit
谢谢你的回答,效果不错!最终采用了在数组中存储集合的方法,只是让我的生活变得更轻松了一点
Sub FillArray()

Dim arr() As Variant
Dim col123 As Collection

Set col123 = New Collection
col123.Add 1, "FirstKey"
col123.Add "Whatever", "Foo"

ReDim arr(1 To 2, 1 To 1) As Variant
arr(1, 1) = 1000
Set arr(2, 1) = col123

ReDim Preserve arr(1 To 2, 1 To 2) As Variant
Set col123 = New Collection
col123.Add "Something", "InSomeKey"
col123.Add "Another thing", "In another key"
arr(1, 2) = 2000
Set arr(2, 2) = col123

End Sub