Excelの新しいインスタンスを作成できますが、新しいインスタンスのOnTime
メソッドを使用する必要があります。 .Run
ステートメントを使用して現在のワークブックからそれらを実行しようとすると、それはスレッドを縛ります。
ここでは例を示しますが、通常の呼び出されたプロシージャでこれをテストし、.EnableEvents
も含めて、イベントハンドラが開かれたときに2番目のブックにイベントハンドラが起動するのを防ぎます。これがなければ、イベントハンドラがあれば、実行時に実行を中断し、実行を一時停止することができます。
Sub main()
Dim oXL As Excel.Application
Dim wb As Workbook
Dim filePath$, fileName$, moduleName$, procName$
'## The locaation of the file you want to open, which contains the macro
filePath = "C:\debug\"
'## The name of the workbook containing the macro
fileName = "book2.xlsm"
'## The name of the module containing the macro:
moduleName = "Module1"
'## The name of the macro procedure
procName = "foo"
'## Create new instance of Excel
Set oXL = New Excel.Application
oXL.Visible = True 'or True, if you prefer
'## Open the file containing the macro
oXL.EnableEvents = False
Set wb = oXL.Workbooks.Open(filePath & "\" & fileName)
oXL.EnableEvents = True
'## Use the OnTime method to hand the thread to other instance of Excel
oXL.Application.OnTime Now + TimeValue("0:00:05"), fileName & "!" & moduleName & "." & procName
'## Inform user that the macro is running in the other workbook/instance of Excel
Debug.Print "The procedure " & procName & " is running in " & fileName
wb.Activate
Set oXL = Nothing
End Sub
実行するための新しい「Excel.Application」インスタンスを作成してください。 – Comintern