vba VB 清除 Scripting.Dictionary 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9836660/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:49:50  来源:igfitidea点击:

VB Clear Scripting.Dictionary object

excelvbaexcel-vba

提问by maephisto

I'm writing an Excel macro, and I'm having trouble clearing a Scripting.Dictionary object:

我正在编写Excel 宏,但在清除Scripting.Dictionary 对象时遇到问题:

Dim test As Integer
test = CompListDict.Count
CompListDict.RemoveAll
Set CompListDict = Nothing
Set CompListDict = CreateObject("Scripting.Dictionary")
Dim test1 As Integer
test1 = CompListDict.Count

Before I do this, I add items to the dictionary, and then i try to clear it, but test1 will equal test and equal the nr of objects I've added.

在我这样做之前,我将项目添加到字典中,然后我尝试清除它,但 test1 将等于 test 并且等于我添加的对象的 nr。

What am I doing wrong ? Thank you!

我究竟做错了什么 ?谢谢!

回答by T. Fabre

If I run the following macro on my workstation, it works :

如果我在工作站上运行以下宏,它会起作用:

        Set compListDict = CreateObject("Scripting.Dictionary")

        compListDict.Add 1, "Test"

        Dim test As Integer
        test = compListDict.Count
        compListDict.RemoveAll

        Set compListDict = Nothing
        Set compListDict = CreateObject("Scripting.Dictionary")

        Dim test1 As Integer
        test1 = compListDict.Count

After running it, test1 equals 0, and test equals 1.

运行后,test1等于0,test等于1。

Make sure you have Option Explicit enabled, and that you don't have any typos in your variable names.

确保您启用了 Option Explicit,并且您的变量名称中没有任何拼写错误。

Also, make sure you don't have an "On Error Resume Next" statement, as it will hide away errors in your code. Try placing "On Error Goto 0" before your code snippet, so that Excel will display any error message.

另外,请确保您没有“On Error Resume Next”语句,因为它会隐藏您代码中的错误。尝试在代码片段之前放置“On Error Goto 0”,以便 Excel 显示任何错误消息。

Since you're setting the variable value to Nothing, and assigning it a new dictionnary object, it should be impossible that it retains the previsouly stored values.

由于您将变量值设置为 Nothing,并为其分配了一个新的字典对象,因此它应该不可能保留先前存储的值。

I also tried running the following code, and it also gives the same results:

我也尝试运行以下代码,它也给出了相同的结果:

        Set compListDict = CreateObject("Scripting.Dictionary")

        compListDict.Add 1, "Test"

        Dim test As Integer
        test = compListDict.Count
        compListDict.RemoveAll

        Dim test1 As Integer
        test1 = compListDict.Count

Hope that helps...

希望有帮助...

回答by assylias

Your code looks ok although the 2 lines where you set your dict to nothing and recreate it are not necessary.

尽管不需要将 dict 设置为空并重新创建它的 2 行,但您的代码看起来不错。

Not sure if it's related but there is a bug in some versions of VBA IDE: if you have added a watch on dict(aKeyThatDoesNotExist)it can lead to tgat key being added to the dict and not being removable. the only solution I know: restart Excel to clear the memory.

不确定它是否相关,但在某些版本的 VBA IDE 中存在一个错误:如果您在dict(aKeyThatDoesNotExist)其上添加了一个监视,则可能导致 tgat 键被添加到 dict 并且无法移除。我知道的唯一解决方案:重新启动 Excel 以清除内存。

EDIT

编辑

For Siddharth: Tested with Excel 2003 & 2010.

对于 Siddharth:使用 Excel 2003 和 2010 进行测试。

Create a new book.
Open VBA IDE.
Type this in the Sheet1 module:

创建一本新书。
打开 VBA IDE。
在 Sheet1 模块中输入:

Option Explicit

Sub test()

    Dim d As Variant
    Set d = CreateObject("Scripting.Dictionary")

    d.Add "a", "a"
    Debug.Print d.Count

    'Add watch here on d and d("b")

    Debug.Print d.Count

End Sub

Run it in step by step mode and when on the commented line, add a watch on dand d("b"). The second Debug.Printwill print 2. So far, you could think that the watch created an entry, which is weird because you would not expect watch to have side effects.

以逐步模式运行它,当在注释行上时,在d和上添加监视d("b")。第二个Debug.Print将打印 2。到目前为止,您可能认为手表创建了一个条目,这很奇怪,因为您不希望手表有副作用。

Run the macro again: the firstDebug.Printwill print 2 and you will notice that the dictionary already has a "b" key.

再次运行宏:第一个Debug.Print将打印 2,您会注意到字典已经有一个“b”键。

Actually, contrary to what I said above, removing the watch on d("b")and rerunning the macro will reset the dictionary properly.

实际上,与我上面所说的相反,移除手表d("b")并重新运行宏将正确重置字典。