vba的新手.如何将字典对象传递给另一个函数.
Sub aaa(dict As Object)
Set dict = CreateObject("Scripting.Dictionary")
...
process dict
End Sub
Sub process(dict As Scripting.Dictionary)
MsgBox dict.Count
End Sub
Run Code Online (Sandbox Code Playgroud)
给出编译错误:未定义用户定义的类型
也,
Set dict = CreateObject("Scripting.Dictionary")
Run Code Online (Sandbox Code Playgroud)
有效,但是
Dim dict As New Scripting.Dictionary
Run Code Online (Sandbox Code Playgroud)
给出,"用户定义的类型未定义"
我使用excel 2010
mwo*_*e02 13
使用时,CreateObject
您在运行时绑定对象(即后期绑定).当你使用As Scripting.Dictionary
对象时,在编译时绑定(即早期绑定).
如果要进行早期绑定,则需要设置对正确库的引用.为此,请转到"工具" - >"引用"...并选择"Microsoft Scripting Runtime"
要避免该错误,请添加Microsoft Scripting Runtime(在工具 - >引用中).
简化示例:
Sub test_dict()
Dim dict As New Scripting.Dictionary
Call process(dict)
End Sub
Sub process_dict(dict As Scripting.Dictionary)
MsgBox dict.Count
End Sub
Run Code Online (Sandbox Code Playgroud)
我将修改此答案以仅使用后期绑定,并使用对象参数:
Sub aaa(dict As Object)
Set dict = CreateObject("Scripting.Dictionary")
...
process dict
End Sub
Sub process(dict As Object)
MsgBox dict.Count
End Sub
Run Code Online (Sandbox Code Playgroud)