【问题描述】vba创建字典对象有哪几种写法?
【小千解答】可以有以下3种:
写法1:前期绑定
先在VBA编辑界面,依次单击【工具】→【引用】,勾选“Microsoft Scripting Runtime”,确定。
代码如下:
Sub xqoffice() Dim Dic As Dictionary Set Dic = New Dictionary Dic.Add "blog", "xqoffice.cn" MsgBox Dic("blog") Set Dic = Nothing End Sub
写法2:后期绑定
Sub xqoffice2() Dim Dic As Object Set Dic = CreateObject("Scripting.Dictionary") Dic.Add "blog", "xqoffice.cn" MsgBox Dic("blog") Set Dic = Nothing End Sub
写法3:使用类的GUID
Sub xqoffice3() Dim Dic As Object Set Dic = CreateObject("new:{EE09B103-97E0-11CF-978F-00A02463E06F}") Dic.Add "blog", "xqoffice.cn" MsgBox Dic("blog") Set Dic = Nothing End Sub
【知识拓展】
Excel帮助文档显示的CreateObject的语法如下:
其中,第一参数必填,为应用程序名称和类,说的更确切一点,应该是DLL的工程名称(库名,本例为Scripting)加类模块名称(类名,本例为Dictionary),也是注册表里GUID对应的ProgID数据。
【关联主题】