2016-10-27 16 views
0

シンプルバージョン参考:動的モジュール変数

モジュールAが公開変数X

を持って、私は名前をハードコーディングせずに、モジュールBからXの値を取得できるようにしたい「モジュールAを」、すなわち(明らかにこれは正しいコードではありません):

MsgBox Modules("Module A").X 

より高度なバージョン

私はTAAA.xlsmと呼ばれるAdd-In/XLSM(トグルすることができます)を持っています。私はRob Boveyのエラー処理システムを使い、それを改善/拡張したい。

私の多くのモジュールが新しいワークブックを作成しています。ユーザーがエラーを受け取った場合は、エラーを送信して自分自身を検査するオプションをユーザーに与える必要があります。私はそれがユーザにプロンプ​​トを表示したいのですが、彼らは「イエス」と言う場合は、エラーハンドラは私を電子メールにOutlookを使用します。

  1. エラーログ
  2. TAAA.xlsm
  3. どれでも子供のワークブックエラーに関連する

私の計画は、エラーの原因となったコードによって作成または使用されたブックを格納する各モジュール用のパブリックワークブックアレイを作成することでした。こうすることで、エラーハンドラが処理するときに、ワークブックを添付するためにパブリック配列にアクセスできます。

TAAAのワークシートにこのデータを格納するのが「よりシンプルな」ソリューションだと思いますが、それは優雅ではありません。

どのようなご意見も大歓迎です!

EDIT 以下の回答に私自身の問題を解決しました。しかし、私の元の質問には良い答えがあるのか​​、それが不可能なのかはまだ不思議です。

+0

私はあなたにはいません:変数 'X'がVBAプロジェクトの任意のモジュールで' Public'として宣言されている場合、そのプロジェクトのすべてのモジュール間で共有されるので、 'X'実際に宣言されているモジュール名を前に置くことはありません – user3598756

+0

各モジュールには独自のワークブック配列があります。しかし私は、私が思い出した明白な解決策を思いついたと思います。私は以下に投稿します –

答えて

0

回想すると、答えは私には明らかです。

中央エラーハンドラは、エラーの原因となったモジュールをどのように知っていますか?プライベートモジュール名文字列を中央エ​​ラーハンドラに渡します。

同様に、私は中央エラーハンドラの別のパラメータとしてブックの配列を渡すことができます!

ので、代わりにこのように見える中央のエラーハンドラ:振り返ってみると

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

かなり明白:

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 

は、私は定義を変更します。無駄な質問を申し訳ありません。