将多个词典引用到工作簿模块

时间:2018-11-18 00:40:27

标签: excel vba dictionary reference

我想从工作簿模块ThisWorkbook调用单独的Sub过程,以在打开工作簿时运行计算。

我正在使用早期绑定来创建字典,然后从该Sub中调用下一个Sub并引用这些字典。

我可以将整个词典创建过程放在一个单独的模块中,从工作簿模块ThisWorkbook中调用它,然后在下一个从工作簿模块调用的Sub过程中引用字典吗?

这就是我所拥有的:

'----------------------------
'Workbook module ThisWorkbook
'----------------------------

Option Explicit
Private Sub Workbook_Open()

With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
End With

Dim MRMT As Workbook
Dim ER As Worksheet

Set MRMT = Excel.Workbooks("MRMT")
Set ER = MRMT.Worksheets("Sheet1")

With ER
    .Name = "Exposure Report"
End With

Import_Exposure_Report MRMT, ER

' I would like to have "Create_Dictionaries" procedure in a separate 
' module and then pass the dictionaries to the Sub below from this workbook module

Import_Historical_Data MRMT, ER  'Key1, Key2, Key3 ect.

With Application
    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    .EnableEvents = True
End With

End Sub

1 个答案:

答案 0 :(得分:1)

Option Explicit
Public Sub Workbook_Open()

    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
        .DisplayAlerts = False
    End With

    Dim MRMT As Workbook
    Dim ER As Worksheet

    Dim CommoditiesDict As New Scripting.Dictionary
    Dim IndexesDict As New Scripting.Dictionary
    Dim StocksDict As New Scripting.Dictionary
    Dim CryptoDict As New Scripting.Dictionary
    Dim BondsDict As New Scripting.Dictionary
    Dim FXDict As New Scripting.Dictionary

    Set MRMT = Excel.Workbooks("MRMTool_V7")
    Set ER = MRMT.Worksheets("Sheet1")

    ER.Name = "Exposure Report"

    With MRMT

        Import_Exposure_Report MRMT, ER

        Create_Dictionaries BondsDict, CryptoDict, CommoditiesDict, IndexesDict, FXDict, StocksDict, MRMT

        Populate_Historical_Data BondsDict, CryptoDict, CommoditiesDict, IndexesDict, FXDict, StocksDict, MRMT, ER

    End With

    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
        .EnableEvents = True
        .DisplayAlerts = True
    End With

End Sub