VBA中的字典用空键值对创建 [英] Dictionary in VBA is created with empty key value pair

查看:690
本文介绍了VBA中的字典用空键值对创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建一个新词典后:

Dim teams as Dictionary
Set teams = New Dictionary


我如何防止这种情况发生或删除这对?这是正常的行为吗?

I noticed that it already contains empty key - value pair (teams.Count returns value of 1). How can I prevent this from happening or delete this pair? Is this normal behaviour?

推荐答案

如果您引用相同的 Microsoft Scripting Runtime 字典 scrrun.dll 如我 - Excel 2010。

If you are referencing the same Microsoft Scripting Runtime dictionary scrrun.dll as me - Excel 2010.

然后下面的代码返回 0 作为 .Count 创建后无条件的项目。

Then the below code returns 0 as the .Count of items after creation with no manipulation.

Sub SD()
    Dim teams As Dictionary
    Set teams = New Dictionary
    Debug.Print teams.Count
End Sub

我不知道你的字典为什么返回 1 的原因,但我认为一个解决方法这将是简单地使用字典对象

I wouldn't know the reason why your dictionary returns 1, however I think a workaround it would be to simply use the .RemoveAll method of the Dictionary object

Sub SD()
    Dim teams As Dictionary
    Set teams = New Dictionary
    teams.RemoveAll
    Debug.Print teams.Count
End Sub

这篇关于VBA中的字典用空键值对创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆