VBA中的散列表/关联数组 [英] Hash Table/Associative Array in VBA
问题描述
我似乎无法找到解释如何在VBA中创建哈希表或关联数组的文档。这是甚至可能的吗?
您可以链接到一篇文章或更好的文章吗?
我认为您正在寻找Microsoft Scripting Runtime库中的Dictionary对象。 (从VBE中的Tools ... References菜单中添加一个对你的项目的引用。)
它几乎适用于任何可以适用于变体的简单值键不能是数组,而试图使它们成为对象没有多大意义。请参阅下面的@Nile中的评论。):
<$ c (c)Dim d As dictionary
Set d =新字典
d(x)= 42
d(42)=四十二
d(CVErr (xlErrValue))=Excel #VALUE!
Set d(101)= New Collection
您也可以使用VBA集合对象if您的需求更简单,您只需要字符串键。
我不知道是否实际上有任何哈希值,所以如果您需要哈希表,像表现。 (编辑:Scripting.Dictionary确实使用哈希表)。
I can't seem to find the documentation explaining how to create a hash table or associative array in VBA. Is it even possible?
Can you link to an article or better yet post the code?
I think you are looking for the Dictionary object, found in the Microsoft Scripting Runtime library. (Add a reference to your project from the Tools...References menu in the VBE.)
It pretty much works with any simple value that can fit in a variant (Keys can't be arrays, and trying to make them objects doesn't make much sense. See comment from @Nile below.):
Dim d As dictionary
Set d = New dictionary
d("x") = 42
d(42) = "forty-two"
d(CVErr(xlErrValue)) = "Excel #VALUE!"
Set d(101) = New Collection
You can also use the VBA Collection object if your needs are simpler and you just want string keys.
I don't know if either actually hashes on anything, so you might want to dig further if you need hashtable-like performance. (EDIT: Scripting.Dictionary does use a hash table internally.)
这篇关于VBA中的散列表/关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!