Per interrogare la struttura del codice reale del progetto è necessario consentire l'accesso al modello di oggetto del progetto VBA (Impostazioni di Excel> Centro protezione> Impostazioni macro quindi aggiungere un riferimento a Microsoft Visual Basic per vX Application Extensibility) dove vX è la versione come 5.3. È possibile utilizzare gli oggetti in questo per identificare quali fogli hanno il codice all'interno di essi.
Tuttavia, consiglierei di farlo in un altro modo.
Invece, scorrere i fogli di lavoro nella cartella di lavoro e poi, all'interno di un involucro di errore, eseguire la macro utilizzando Application.Run
noti che sarebbe meglio la pratica di refactoring il codice e mettere tutto in uno standard modulo, quindi passare nei fogli di lavoro come argomenti (vedi il mio secondo esempio)
esempio:
'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
fonte
2013-08-08 15:35:02