vba 在调用 .add() 之前添加项目的字典对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11400898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dictionary object adding items before .add() is called
提问by riddley_w
I am using a dictionary object from the MS Scripting Runtime libraryto store a series of arrays and perform operations on the array cells as necessary. There is a for loop to go through the process of creating all of these entries. My issue is that when using the .exists
property, it is returning True
even before the item has been added.
我正在使用MS Scripting Runtime 库中的字典对象来存储一系列数组并根据需要对数组单元执行操作。有一个 for 循环来完成创建所有这些条目的过程。我的问题是,在使用该.exists
属性时,它True
甚至在添加项目之前就返回了。
Closer debugging indicates that the key is being added to the dictionary at the beginning of the for loop, even though no .add
command is used and will not be used until the end of the loop.
更仔细的调试表明在 for 循环开始时将键添加到字典中,即使没有.add
使用任何命令并且直到循环结束才会使用。
I have tried a few different configurations, but here is a simple example that fails:
我尝试了几种不同的配置,但这是一个失败的简单示例:
Dim dTotals As Dictionary
Set dTotals = New Dictionary
dTotals.CompareMode = BinaryCompare
For Each cell In rAppID
If Not dTotals.Exists(cell) Then
Set rAppIDCells = Find_Range(cell, rAppID)
Set rAppIDValues = rAppIDCells.Offset(0, 6)
dAppIDTotal = WorksheetFunction.Sum(rAppIDValues)
dTotals.Add Key:=cell.Value, Item:=dAppIDTotal
End If
Next cell
Where each cell contains a string / unique id. At the If statement, the code is returning false, even on the first iteration.
每个单元格包含一个字符串/唯一 ID。在 If 语句中,代码返回 false,即使在第一次迭代时也是如此。
采纳答案by Vic
In the official documentation?? for the scripting runtime it says "If key is not found when attempting to return an existing item, a new key is created and its corresponding item is left empty."
在官方文档中?? 对于脚本运行时,它说“如果在尝试返回现有项目时未找到密钥,则会创建一个新密钥并将其对应的项目留空。”
...and yea, when you're debugging in a loop, it appears to pop right out of the sky before the '.exists' function is even called. All is well...
...是的,当您在循环中调试时,它似乎在“.exists”函数被调用之前就突然出现了。一切都很好...
Instead of attempting to add the item that just got added, as in:
而不是尝试添加刚刚添加的项目,如下所示:
dTotals.Add Key:=cell.Value, Item:=dAppIDTotal
...just set the empty object currently at your key to your new one:
...只需将当前位于您键上的空对象设置为新对象:
dTotals(cell.Value) = dAppIDTotal
So your code block becomes:
所以你的代码块变成:
If Not dTotals.Exists(cell) Then
Set rAppIDCells = Find_Range(cell, rAppID)
Set rAppIDValues = rAppIDCells.Offset(0, 6)
dAppIDTotal = WorksheetFunction.Sum(rAppIDValues)
dTotals(cell.Value) = dAppIDTotal
End If
Voila. I tend to rediscover this "feature" on every revisit to VBA. You may also notice the effects of it if you are having a memory leak caused by adding new keys that you do not intend to store.
瞧。我倾向于在每次重新访问 VBA 时重新发现这个“功能”。如果由于添加不打算存储的新密钥而导致内存泄漏,您可能还会注意到它的影响。
回答by Mike
I had this problem manifest itself while debugging when I had a watch that attempted to return the "missing" key's item. Actually, further frustrated debugging had the same problem when I literally had a watch for the [scriptingdictonaryObject].exists() condtional); I suggest that the "missing" key is added because of the watch. When I removed the watch and instead created a temporary worksheet to copy the array to while running, the unwanted keys were no longer added.
当我有一个手表试图返回“丢失”键的项目时,我在调试时遇到了这个问题。实际上,当我真正监视 [scriptingdictonaryObject].exists() 条件时,进一步受挫的调试也遇到了同样的问题;我建议由于手表的原因添加“丢失”键。当我移除手表并创建一个临时工作表以在运行时将数组复制到时,不再添加不需要的键。