excel vba 导入 system.collections.hashmap 到一个模块中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19424673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
excel vba import system.collections.hashmap into a module
提问by kiltek
From the inside of my MS Excel 2010installation I have opened the Visual Basic Editor. (Tab Developer Tools -> Visual Basic)
从我的MS Excel 2010安装内部,我打开了Visual Basic Editor。(选项卡开发人员工具 -> Visual Basic)
Inside the Visual Basic Editori right-clicked into the Project Windowand created a module. (Insert -> Module)
在Visual Basic 编辑器中,我右键单击项目窗口并创建了一个模块. (插入 -> 模块)
Then i typed the following code into the Code Window:
然后我在代码窗口中输入以下代码:
Sub myFunction()
'do some stuff to my table cells, not important
End Sub
After coding a lot of things inside the function body, i thought i was in need for a Hashmap, which needs to be accessed inside that given function body. Unfortunately, i could not importthe class needed by doing this:
在函数体内编码了很多东西后,我认为我需要一个 Hashmap,它需要在给定的函数体内访问。不幸的是,我无法通过这样做导入所需的类:
Imports System.Collections
Sub myFunction()
'do some stuff to my table cells, not important
End Sub
The error messageappears when launching the module by pressing F5. And it says (translated from geman to english by me): "Error while Compiling: illegal outside of a procedure".
的错误消息通过按F5启动模块时出现。它说(由我从德语翻译成英语):“编译时出错:程序外非法”。
How is it possible to have something imported in VBA, when the code is structured like above?I'am usually a java or pythonperson. You can also re-structure the code, if the function still executes.
当代码结构如上时,如何在 VBA 中导入某些内容?我通常是一个java 或 python人。如果函数仍在执行,您还可以重新构建代码。
回答by
In this case you either use a
在这种情况下,您要么使用
Collectionclass
收藏类
or
或者
Dictionaryclass
字典类
Collection
class is built-ininto VBA so you do not need to add external references to be able to use it. You can simply declare it
Collection
类内置在VBA 中,因此您无需添加外部引用即可使用它。你可以简单地声明它
Dim c as Collection
Set c = new Collection
Collection
exposes 4 methods: add, count, item, remove
so it may not be sufficient for you.
Collection
公开 4 种方法:add, count, item, remove
所以它可能对你来说还不够。
Customizing, expanding Collection class
Customizing, expanding Collection class
If you want to use something like a HashTable/HashMapthen add references to Microsoft Scripting Runtime
by clicking Tools
and References
in the VBE window
如果你想使用像HashTable/HashMap这样的东西,然后Microsoft Scripting Runtime
通过在 VBE 窗口中单击Tools
和添加引用References
then you can use early-binding and intellisense with the Dictionary
class
然后你可以在Dictionary
类中使用早期绑定和智能感知
Dim d as Dictionary
Set d = new Dictionary
Or use late-binding
或者使用后期绑定
Dim d as Object
set d = CreateObject("Scripting.Dictionary")
I would go for the early-binding (first example - adding references to VBA project) so you can use VBA Intellisense.
我会选择早期绑定(第一个示例 - 添加对 VBA 项目的引用),以便您可以使用 VBA Intellisense。
You can view Dictionary
or Collection
class using the Object Browser - simply hit F2in the VBE window and type in Dictionary
or Collection
您可以使用对象浏览器查看Dictionary
或Collection
分类 - 只需点击F2VBE 窗口并输入Dictionary
或Collection
Useful read about the Dictionary class
有用的阅读 Dictionary class