VB清除Scripting.Dictionary对象 [英] VB Clear Scripting.Dictionary object
问题描述
我正在写一个 Excel宏,而且我无法清除 Scripting.Dictionary对象:
I'm writing an Excel macro, and I'm having trouble clearing a Scripting.Dictionary object:
Dim test As Integer
test = CompListDict.Count
CompListDict.RemoveAll
Set CompListDict = Nothing
Set CompListDict = CreateObject("Scripting.Dictionary")
Dim test1 As Integer
test1 = CompListDict.Count
在我这样做之前,我将项目添加到字典,然后我尝试清除它,但是test1将等于测试并等于我添加的对象的nr。
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.
我做错了什么?
谢谢!
What am I doing wrong ? Thank you!
推荐答案
如果我在我的工作站上运行以下宏,它的工作原理是:
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
运行它后,test1等于0,测试等于1.
After running it, test1 equals 0, and test equals 1.
确保启用了Option Explicit,并且您的变量名称中没有任何打字错误。
Make sure you have Option Explicit enabled, and that you don't have any typos in your variable names.
此外,请确保您没有On Error Resume Next语句,因为它会隐藏代码中的错误。尝试在代码片段之前放置On Error Goto 0,以便Excel将显示任何错误消息。
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.
由于您将变量值设置为Nothing,并且分配它是一个新的词典对象,它应该是不可能的,它保留了预先存储的值。
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.
我还尝试运行以下代码,它也给出了相同的结果: / p>
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
希望有帮助...
这篇关于VB清除Scripting.Dictionary对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!