VBA中的哈希表/关联数组 [英] Hash Table/Associative Array in VBA

查看:56
本文介绍了VBA中的哈希表/关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到解释如何在 VBA 中创建哈希表或关联数组的文档.甚至有可能吗?

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?

推荐答案

我认为您正在寻找在 Microsoft Scripting Runtime 库中找到的 Dictionary 对象.(从 VBE 中的工具...参考菜单添加对您的项目的引用.)

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.)

它几乎适用于任何可以适合变体的简单值(键不能是数组,并且试图使它们成为对象没有多大意义.请参阅下面来自@Nile 的评论.):

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

如果您的需求更简单并且只需要字符串键,您也可以使用 VBA 集合对象.

You can also use the VBA Collection object if your needs are simpler and you just want string keys.

我不知道两者是否真的对任何东西进行了哈希处理,因此如果您需要类似哈希表的性能,您可能需要进一步挖掘.(Scripting.Dictionary 确实使用了 内部哈希表.)

I don't know if either actually hashes on anything, so you might want to dig further if you need hashtable-like performance. ( Scripting.Dictionary does use a hash table internally.)

这篇关于VBA中的哈希表/关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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