在VBA,Excel中自动创build不同的,可识别的集合

所以我有大量的玩具车,我把它们存放在一个系列里(因为一开始我不知道会有多less辆卡车),而且每辆卡车都有很多属性,比如销售数量,价格,轮底等。这些卡车的一个特定属性是重量限制,即这个玩具可以携带多less重量,并且只有一定数量的不同的重量限制,但是当我开始时我不知道有多lessmacros观。

我想要做的就是循环收集,并将卡车添加到特定的重量不同的collections,但如上所述,我不知道会有多less不同的重量限制。

所以我想我的问题是我怎么能自动创build可以轻易识别的集合,每个集合是在一个新的重量限制卡车发现循环所有的卡车时创build的?

举个例子,如果我有10辆卡车,其中6辆可以运载2公斤,4辆可以运载3公斤,我想要两辆将2公斤和3公斤卡车分开的集合。

我曾经想过创build一个3维数组,但是这将涉及到很多“空白空间”,其中一些权重限制比其他更常见,所以更多的代码来处理,这是不理想的。 至于创build一个参差不齐的数组,我遇到了不知道如何自动创build独特的,容易识别的数组的问题。

理想情况下,我想要做的是dynamic创build一个2维数组,其中第一行是权重限制,第二行是一个集合的引用(当新的权重限制添加到数组时自动创build),但我不要认为这是可能的…(由于ReDim Preserve,包含重量限制的行)

以下是我目前正在做的事情,但显然这并不理想:(另外,每次的重量限制不会上升2次)

For Each v In collTruck If v.WeightLimit = 8 Then coll8.Add v ElseIf v.WeightLimit = 10 Then coll10.Add v ElseIf v.WeightLimit = 12 Then coll12.Add v ElseIf v.WeightLimit = 14 Then coll14.Add v Else: collOtherW.Add v End If Next v Set collWeights = New Collection collWeights.Add c8 collWeights.Add c10 collWeights.Add c12 collWeights.Add c14 collWeights.Add cOtherW 

下面是将卡车集合作为参数的function,并返回包含与不同重量限制相同数量的子集合的集合。 每个这样的子集包含所有具有这个特定重量限制的卡车。

 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 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