多个Excel VBA例程访问相同的对象?

多个Excel VBA例程访问相同的对象?,excel,vba,Excel,Vba,我有以下两个子例程: Rem Attribute VBA_ModuleType=VBAModule Sub FixPlatformsSelection() Dim fndList As Object Set fndList = CreateObject("Scripting.Dictionary") fndList.Add "3DO Interactive Multiplayer", "3DO" fndList.Add "Nintendo 3DS", "3DS"

我有以下两个子例程:

Rem Attribute VBA_ModuleType=VBAModule
Sub FixPlatformsSelection()
    Dim fndList As Object
    Set fndList = CreateObject("Scripting.Dictionary")
    fndList.Add "3DO Interactive Multiplayer", "3DO"
    fndList.Add "Nintendo 3DS", "3DS"
    fndList.Add "Ajax", "AJAX"
    fndList.Add "Xerox Alto", "ALTO"
    fndList.Add "Amiga CD32", "AMI32"
    fndList.Add "Amiga", "AMI"
    fndList.Add "Apple I", "APPI"
    fndList.Add "Apple IIe", "APPIIE"
    fndList.Add "Apple IIGS", "APPGS"
    fndList.Add "Apple II Plus", "APPII+"
    fndList.Add "Apple II series", "APPII"
    fndList.Add "Apple II", "APPII"

    For Each strKey In fndList.Keys()
        Selection.Replace What:=strKey, Replacement:=fndList(strKey), _
          LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
          SearchFormat:=False, ReplaceFormat:=False
    Next strKey
End Sub

Rem Attribute VBA_ModuleType=VBAModule
Sub FixPlatformsWorkbook()
    Dim fndList As Object
    Set fndList = CreateObject("Scripting.Dictionary")
    fndList.Add "3DO Interactive Multiplayer", "3DO"
    fndList.Add "Nintendo 3DS", "3DS"
    fndList.Add "Ajax", "AJAX"
    fndList.Add "Xerox Alto", "ALTO"
    fndList.Add "Amiga CD32", "AMI32"
    fndList.Add "Amiga", "AMI"
    fndList.Add "Apple I", "APPI"
    fndList.Add "Apple IIe", "APPIIE"
    fndList.Add "Apple IIGS", "APPGS"
    fndList.Add "Apple II Plus", "APPII+"
    fndList.Add "Apple II series", "APPII"
    fndList.Add "Apple II", "APPII"

    For Each sht In ActiveWorkbook.Worksheets
    For Each strKey In fndList.Keys()
        sht.Cells.Replace What:=strKey, Replacement:=fndList(strKey), _
          LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
          SearchFormat:=False, ReplaceFormat:=False
    Next strKey
    Next sht
End Sub
如何从子例程中删除
fndList
字典,并将其移动到其他地方,以便所有子例程也可以访问它?我有两个例程需要这个字典,不想维护同一代码的两个副本。在VBA中是否有放置“全局”变量的特殊位置?谢谢

[编辑]

我尝试将声明置于程序之外:

Public fndList As Object
Set fndList = CreateObject("Scripting.Dictionary")
fndList.Add "3DO Interactive Multiplayer", "3DO"
fndList.Add "Nintendo 3DS", "3DS"
fndList.Add "Ajax", "AJAX"
fndList.Add "Xerox Alto", "ALTO"
fndList.Add "Amiga CD32", "AMI32"
fndList.Add "Amiga", "AMI"
fndList.Add "Apple I", "APPI"
fndList.Add "Apple IIe", "APPIIE"
fndList.Add "Apple IIGS", "APPGS"
fndList.Add "Apple II Plus", "APPII+"
fndList.Add "Apple II series", "APPII"
fndList.Add "Apple II", "APPII"

Rem Attribute VBA_ModuleType=VBAModule
Sub FixPlatformsSelection()
    For Each strKey In fndList.Keys()
    Selection.Replace What:=strKey, Replacement:=fndList(strKey), _
      LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
      SearchFormat:=False, ReplaceFormat:=False
    Next strKey
End Sub

但是我得到了一个编译错误:无效的外部过程。

您可以在任何
Sub
函数
外部创建模块级变量:

Private fndList As Scripting.Dictionary
如果需要多个文件(又称模块)访问该变量,则将该变量声明为
Public

Public fndList As Scripting.Dictionary

虽然可以在过程之外声明变量,但不能在过程之外执行语句(您将得到
无效的外部过程。
错误消息)。因此,初始化代码必须位于第三个

Sub InitDictionary
    If Not fndList Is Nothing Then Exit Sub
    Set fndList = New Scripting.Dictionary
    fndList.Add "3DO Interactive Multiplayer", "3DO"
    fndList.Add "Nintendo 3DS", "3DS"
    fndList.Add "Ajax", "AJAX"
    fndList.Add "Xerox Alto", "ALTO"
    fndList.Add "Amiga CD32", "AMI32"
    fndList.Add "Amiga", "AMI"
    fndList.Add "Apple I", "APPI"
    fndList.Add "Apple IIe", "APPIIE"
    fndList.Add "Apple IIGS", "APPGS"
    fndList.Add "Apple II Plus", "APPII+"
    fndList.Add "Apple II series", "APPII"
    fndList.Add "Apple II", "APPII"
End Sub
如果需要,它将初始化字典

然后,从其他每个子过程调用初始化
Sub

Sub FixPlatformsSelection()
    InitDictionary

    For Each strKey In fndList.Keys()
    '...
    Next
End Sub

Sub FixPlatformsWorkbook()
    InitDictionary

    For Each sht In ActiveWorkbook.Worksheets
        '...
    Next sht
End Sub

参考资料:


您可以在任何
子功能或
功能之外创建模块级变量

Private fndList As Scripting.Dictionary
如果需要多个文件(又称模块)访问该变量,则将该变量声明为
Public

Public fndList As Scripting.Dictionary

虽然可以在过程之外声明变量,但不能在过程之外执行语句(您将得到
无效的外部过程。
错误消息)。因此,初始化代码必须位于第三个

Sub InitDictionary
    If Not fndList Is Nothing Then Exit Sub
    Set fndList = New Scripting.Dictionary
    fndList.Add "3DO Interactive Multiplayer", "3DO"
    fndList.Add "Nintendo 3DS", "3DS"
    fndList.Add "Ajax", "AJAX"
    fndList.Add "Xerox Alto", "ALTO"
    fndList.Add "Amiga CD32", "AMI32"
    fndList.Add "Amiga", "AMI"
    fndList.Add "Apple I", "APPI"
    fndList.Add "Apple IIe", "APPIIE"
    fndList.Add "Apple IIGS", "APPGS"
    fndList.Add "Apple II Plus", "APPII+"
    fndList.Add "Apple II series", "APPII"
    fndList.Add "Apple II", "APPII"
End Sub
如果需要,它将初始化字典

然后,从其他每个子过程调用初始化
Sub

Sub FixPlatformsSelection()
    InitDictionary

    For Each strKey In fndList.Keys()
    '...
    Next
End Sub

Sub FixPlatformsWorkbook()
    InitDictionary

    For Each sht In ActiveWorkbook.Worksheets
        '...
    Next sht
End Sub

参考资料:



是否希望词典在作用域中为模块级?或者,您是否需要在范围内将其设置为项目级别?还是只想将字典作为参数从一个子例程传递到另一个子例程?也许可以发布更多的代码,以便我们能够明智地决定您应该使用什么。它甚至需要是一个字典,还是仅仅是Excel中的一个“表”?@YowE3K我认为使用字典要比操作Excel对象轻得多。我建议添加对Microsoft脚本运行时的引用;这将允许您将Dim dict作为新脚本编写。Dictionary
,并将提供有关Dictionary属性和方法的智能感知。添加Microsoft Scripting Runtime reference的目的是简化VBA代码的编写,而与宏的运行方式无关。目前,VBA编辑器不知道
findLst
应该指向
字典
,方法是
Add
,属性是
CompareMode
。指定
fndList
的类型后,编辑器会警告您尝试调用不存在的方法或传递给方法的错误参数。是否希望字典在作用域中为模块级?或者,您是否需要在范围内将其设置为项目级别?还是只想将字典作为参数从一个子例程传递到另一个子例程?也许可以发布更多的代码,以便我们能够明智地决定您应该使用什么。它甚至需要是一个字典,还是仅仅是Excel中的一个“表”?@YowE3K我认为使用字典要比操作Excel对象轻得多。我建议添加对Microsoft脚本运行时的引用;这将允许您将Dim dict作为新脚本编写。Dictionary
,并将提供有关Dictionary属性和方法的智能感知。添加Microsoft Scripting Runtime reference的目的是简化VBA代码的编写,而与宏的运行方式无关。目前,VBA编辑器不知道
findLst
应该指向
字典
,方法是
Add
,属性是
CompareMode
。一旦指定了
fndList
的类型,编辑器就会警告您尝试调用不存在的方法,或者错误的参数传递给这些方法。我得到一个“编译错误:未定义用户定义的类型”
Dim fndList As Scripting.Dictionary
@posfan12出现错误-通过VB中的工具->引用菜单添加对Microsoft脚本运行时的引用Editor@posfan12我猜
FixPlatformsSelection
FixPlatformsWorkbook
不在同一个VBA文件中;这就是为什么
Dim
(或
Private
)不够用的原因。@postfan在VBA中,每个文件包含一个模块,每个模块仅限于一个文件。还请注意,我已经添加了一个Microsoft文档的链接,该链接涉及脚本.字典和VBA中的作用域规则。@posfan12,这取决于您的具体情况。您可能希望将相关的
Sub
s/
函数组组织到单独的模块/文件中,在这种情况下,您希望变量是
Public
Dim fndList As Scripting.Dictionary
@posfan12出现错误-通过VB中的工具->引用菜单添加对Microsoft脚本运行时的引用Editor@posfan12我猜
FixPlatformsSelection
FixPlatformsWorkbook
不在同一个VBA文件中;这就是为什么
Dim
(或
Private
)不够用的原因。@postfan在VBA中,每个文件包含一个模块,每个模块仅限于一个文件。还请注意,我已经添加了一个Microsoft文档的链接,该链接涉及脚本.字典和VBA中的作用域规则。@posfan12,这取决于您的具体情况。你可能想要组织一个r