更改字典中集合中项目的值

我正在尝试创build一个包含每个键的集合的字典。 原因是我想稍后从相同的键检索多个值。 在这个例子中,我想要有唯一键的总值(val)以及出现次数(n):

sub update() Dim dict As Dictionary Dim coll As Collection Set dict = New Dictionary Set coll = New Collection coll.Add 100, "val" coll.Add 3, "n" dict.Add "coll", coll Debug.Print dict.item("coll")("val") Debug.Print dict.item("coll")("n") 

这到目前为止工作正常,当我尝试更新集合中的值(对象不支持此)时,会发生问题:

 dict.item("coll")("val") = dict.item("coll")("val") + 100 

我试过的:

如果我使用数组而不是集合,没有错误,但值不会更改。 它只适用于读取集合到variables,更改值,创build一个新的集合,从字典中删除旧的并添加新的集合。

有没有什么办法像上面的方法一样? 我也很乐意为这个任务提供另一种解决scheme。

一旦你添加了一个项目的集合,你不能轻易改变它。 这样的expression:

 coll("n") = 5 

将导致运行时错误“424”:需要对象

你可以自己检查下面的简单例子:

 Sub testCol() Dim col As New VBA.Collection Call col.Add(1, "a") col("a") = 2 '<-- this line will cause Run-time error '424' End Sub 

更改给定集合中指定键的值的唯一方法是删除此值并使用相同的键添加另一个值。

下面是一个简单的例子,说明如何使用键[ a ]从1到2更改分配给集合的值:

 Sub testCol() Dim col As New VBA.Collection With col Call .Add(1, "a") Call .Remove("a") Call .Add(2, "a") End With End Sub 

下面是您修改的代码,以便您可以更改分配给集合中给定键的值:

 Sub update() Dim dict As Dictionary Dim coll As Collection Set dict = New Dictionary Set coll = New Collection coll.Add 100, "val" coll.Add 3, "n" dict.Add "coll", coll Debug.Print dict.Item("coll")("val") Debug.Print dict.Item("coll")("n") 'This works fine so far, the problem occurs when I try to update the value in the collection (object doesn't support this): Dim newValue As Variant With dict.Item("coll") newValue = .Item("val") + 100 On Error Resume Next '<---- [On Error Resume Next] to avoid error if there is no such key in this collection yet. Call .Remove("val") On Error GoTo 0 Call .Add(newValue, "val") End With End Sub 

也许这不是很优雅,但也许你可以写一个sub来更新一个集合:

 Sub UpdateCol(ByRef C As Collection, k As Variant, v As Variant) On Error Resume Next C.Remove k On Error GoTo 0 C.Add v, k End Sub 

像这样使用:

 Sub Update() Dim dict As Dictionary Dim coll As Collection Set dict = New Dictionary Set coll = New Collection coll.Add 100, "val" coll.Add 3, "n" dict.Add "coll", coll Debug.Print dict.Item("coll")("val") Debug.Print dict.Item("coll")("n") UpdateCol dict.Item("coll"), "val", dict.Item("coll")("val") + 100 Debug.Print dict.Item("coll")("val") End Sub 

如预期的输出:

 100 3 200 

这是一个使用用户定义的对象(类)的方法。 好吧,你可以适应这个问题。

重命名类模块cMyStuff或其他有意义的东西。

类模块

 Option Explicit Private pTotalVal As Long Private pCounter As Long Public Property Get TotalVal() As Long TotalVal = pTotalVal End Property Public Property Let TotalVal(Value As Long) pTotalVal = Value End Property Public Property Get Counter() As Long Counter = pCounter End Property Public Property Let Counter(Value As Long) pCounter = Value End Property 

常规模块

 Option Explicit Sub Update() Dim cMS As cMyStuff, dMS As Dictionary Dim I As Long Set dMS = New Dictionary For I = 1 To 3 Set cMS = New cMyStuff With cMS .Counter = 1 .TotalVal = I * 10 If Not dMS.Exists("coll") Then dMS.Add "coll", cMS Else With dMS("coll") .TotalVal = .TotalVal + cMS.TotalVal .Counter = .Counter + 1 End With End If End With Next I With dMS("coll") Debug.Print "Total Value", .TotalVal Debug.Print "Counter", .Counter End With End Sub 

结果立即窗口

 Total Value 60 Counter 3