动态引用模块变量

时间:2016-10-27 16:37:57

标签: excel vba excel-vba

简单版

模块A有一个公共变量X

我希望能够从模块B获取X的值,而无需硬编码名称“模块A”,即(显然这不是正确的代码):

MsgBox Modules("Module A").X

更多高级版

我有一个名为TAAA.xlsm的加载项/ XLSM(它可以自行切换)。我使用Rob Bovey的错误处理系统,并希望改进/扩展它。

我的很多模块都会创建新的工作簿。如果用户收到错误,我想给他们选择发送错误来检查自己。我希望它能提示用户,如果他们说“是”,错误处理程序会使用Outlook给我发电子邮件:

  1. 错误记录
  2. TAAA.xlsm
  3. 与错误相关的任何子工作簿
  4. 我的计划是为每个模块安装一个公共工作簿数组,它将存储导致错误的代码创建/使用的任何工作簿。这样,当错误处理程序处理时,它可以访问该公共数组以附加工作簿。

    我认为一个“更简单”的解决方案是将这些数据存储在TAAA的工作表中,尽管它并不优雅。

    任何想法都会非常感激!

    修改 我在下面的答案中解决了我自己的问题。但是,如果对我原来的问题有一个很好的答案或者那是不可能的话,我仍然很好奇。

1 个答案:

答案 0 :(得分:0)

回想起来,答案对我来说非常明显。

中央错误处理程序如何知道错误来自哪个模块?通过私有模块名称字符串传递给中央错误处理程序。

同样,我可以将工作簿数组作为另一个参数传递给中央错误处理程序!

所以代替中央错误处理程序看起来像这样:

    {
      "to" : "user_registration_id",
      "priority" : "normal",
      "notification" : {
        "body" : "You have 3 messages in your inbox",
        "title" : "NewsMagazine.com",
        "icon" : "new",
      }
    }

我会将定义更改为:

Public Function bCentralErrorHandler( _
            ByVal sModule As String, _
            ByVal sProc As String, _
            Optional ByVal sFile As String, _
            Optional ByVal bEntryPoint As Boolean) As Boolean

    Static sErrMsg As String

    Dim iFile As Integer
    Dim lErrNum As Long
    Dim sFullSource As String
    Dim sPath As String
    Dim sLogText As String

    ' Grab the error info before it's cleared by
    ' On Error Resume Next below.
    lErrNum = Err.Number
    ' If this is a user cancel, set the silent error flag
    ' message. This will cause the error to be ignored.
    If lErrNum = glUSER_CANCEL Then sErrMsg = msSILENT_ERROR
    ' If this is the originating error, the static error
    ' message variable will be empty. In that case, store
    ' the originating error message in the static variable.
    If Len(sErrMsg) = 0 Then sErrMsg = Err.Description

    ' We cannot allow errors in the central error handler.
    On Error Resume Next

    ' Load the default filename if required.
    If Len(sFile) = 0 Then sFile = ThisWorkbook.Name

    ' Get the application directory.
    sPath = ThisWorkbook.Path
    If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"

    ' Construct the fully-qualified error source name.
    sFullSource = "[" & sFile & "]" & sModule & "." & sProc

    ' Create the error text to be logged.
    sLogText = "  " & Application.UserName & sFullSource & ", Error " & _
                        CStr(lErrNum) & ": " & sErrMsg

    ' Open the log file, write out the error information and
    ' close the log file.
    iFile = FreeFile()
    Open sPath & msFILE_ERROR_LOG For Append As #iFile
    Print #iFile, Format$(Now(), "mm/dd/yy hh:mm:ss"); sLogText
    If bEntryPoint Then Print #iFile,
    Close #iFile

    ' Do not display or debug silent errors.
    If sErrMsg <> msSILENT_ERROR Then

        ' Show the error message when we reach the entry point
        ' procedure or immediately if we are in debug mode.
        If bEntryPoint Or gbDEBUG_MODE Then
            Application.ScreenUpdating = True
            MsgBox sErrMsg, vbCritical, gsAPP_NAME
            ' Clear the static error message variable once
            ' we've reached the entry point so that we're ready
            ' to handle the next error.
            sErrMsg = vbNullString
        End If

        ' The return vale is the debug mode status.
        bCentralErrorHandler = gbDEBUG_MODE

    Else
        ' If this is a silent error, clear the static error
        ' message variable when we reach the entry point.
        If bEntryPoint Then sErrMsg = vbNullString
        bCentralErrorHandler = False
    End If

End Function

回想起来相当明显。对不起浪费的问题。

相关问题