プロジェクトの実際のコード構造を照会するには、VBA プロジェクト オブジェクト モデルへのアクセスを許可する必要があります ([Excel 設定] > [セキュリティ センター] > [マクロ設定] で、Microsoft Visual Basic for Application Extensibility vX への参照を追加します)。vX は 5.3 のようなバージョンです。 . このオブジェクトを使用して、どのシートにどのコードが含まれているかを識別できます。
ただし、別の方法で行うことをお勧めします。
代わりに、ブック内のワークシートを反復処理してから、エラー ラッパー内で、Application.Run を使用してマクロを実行します。
コードをリファクタリングしてすべてを標準モジュールに入れてから、ワークシートを引数として渡す方が良いことに注意してください (2 番目の例を参照)。
例えば:
'With code distributed in each worksheet
Sub blah()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
On Error Resume Next
Application.Run ws.CodeName & ".CollectInfoMacro"
If Err.Number = 1004 Then Debug.Print "Skipping "; ws.Name; ", No macro defined"
On Error GoTo 0
Next ws
End Sub
'Otherwise, better practice would be to refactor
'and not have code on each sheet, instead in a standard module:
Sub blahblah()
Dim ws As Worksheet
Dim results As Collection
Set results = New Collection
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" Then 'or whatever
results.Add getYourInfoForTheSummary(ws), ws.Name
End If
Next ws
'Process your results (i.e. dump to the summary worksheet etc)
...
End Sub
Function getYourInfoForTheSummary(ws As Worksheet) As Collection 'or return whatever
Dim results As Collection
Set results = New Collection
With ws
'do something
End With
Set getYourInfoForTheSummary = results 'or whatever
End Function