excel vba 将 system.collections.hashmap 导入模块

2024-04-27

从我的内心微软 Excel 2010安装我已经打开了Visual Basic 编辑器。 (选项卡开发工具 -> Visual Basic)

在 - 的里面Visual Basic 编辑器我右键单击进入项目窗口并创建了一个module。 (插入 -> 模块)

然后我将以下代码输入到代码窗口:

Sub myFunction()
    'do some stuff to my table cells, not important
End Sub


在函数体内编码了很多东西之后,我认为我需要一个哈希图,它需要在给定的函数体内访问。不幸的是,我无法导入这样做所需的类:

Imports System.Collections
Sub myFunction()
    'do some stuff to my table cells, not important
End Sub

The 错误信息按 F5 启动模块时会出现。上面写着(由我从德语翻译成英语):“编译时出错:程序外非法”.

当代码结构如上所示时,怎么可能在 VBA 中导入某些内容呢?我通常是一个java或python人。如果函数仍然执行,您还可以重新构造代码。


在这种情况下,您可以使用

收藏 class

or

字典 class


Collection类是built-in到 VBA 中,因此您无需添加外部引用即可使用它。你可以简单地声明它

Dim c as Collection
Set c = new Collection

Collection暴露4个方法:add, count, item, remove所以这对你来说可能还不够。

Customizing, expanding Collection class https://stackoverflow.com/questions/19373081/how-to-use-the-implements-in-excel-vba/19379641#19379641


如果你想使用类似的东西哈希表/哈希映射然后添加引用Microsoft Scripting Runtime通过点击Tools and References在VBE窗口中

那么你可以使用早期绑定和智能感知Dictionary class

Dim d as Dictionary
Set d = new Dictionary

或者使用后期绑定

Dim d as Object
set d = CreateObject("Scripting.Dictionary")

我会选择早期绑定(第一个示例 - 添加对 VBA 项目的引用),以便您可以使用 VBA Intellisense。

You can view Dictionary or Collection class using the Object Browser - simply hit F2 in the VBE window and type in Dictionary or Collection

有用的阅读Dictionary class http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/A_3391-Using-the-Dictionary-Class-in-VBA.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

excel vba 将 system.collections.hashmap 导入模块 的相关文章

随机推荐