在Excel-VBA中创建公共对象的最佳做法? [英] Best practice for creating a public object in Excel-VBA?

查看:179
本文介绍了在Excel-VBA中创建公共对象的最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建应用程序的所有成员可访问的 Excel-VBA 数据对象(字典,列表等)的最佳做法是什么?是否应该将其声明为单独的模块或类模块?

What is the best practice for creating an Excel-VBA data object (dictionary, list, etc.) which is accessible by all members of the application? Should it be declared as a separate module or a class module?

例如,我想创建一个字典对象,不同的子例程将要检查用户输入(如果包含或不)。这个字典对象应该是自己的模块,类模块还是包含使用它的子程序的模块的一部分?

For example, I want to create a dictionary object which different subroutines will want to check a user input against (if it contains or not). Should this dictionary object be its own module, class module, or part of the module which contains the subroutines who use it?

注意:这个问题是检查的最佳做法如果值是vba中的列表的成员?

推荐答案

可以使用以下构造(声明您的 myList 对象作为公开在您的模块的顶部):

You can use following construction (declare your myList object as Public in the top of your module):

Public myList As Object

Sub Main()

    Call InitializeList

    'Do something with your Dictionary object

End Sub

Sub InitializeList()
    If Not myList Is Nothing Then Exit Sub

    Set myList = CreateObject("Scripting.Dictionary")

    myList.Add "item1", 1
    myList.Add "item2", 2
    myList.Add "item3", 3
End Sub

这篇关于在Excel-VBA中创建公共对象的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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