Excel의 새 인스턴스를 만들 수 있지만 OnTime
새 인스턴스의 메서드를 사용해야합니다. .Run
문을 사용하여 현재 통합 문서에서 실행하려고하면 스레드가 연결됩니다.
예를 들어, 일반적인 호출 프로 시저에 대해이 테스트를 수행했으며 이벤트 처리기를 열 때 이벤트 처리기가 두 번째 통합 문서에서 실행되는 것을 방지하기 위해 .EnableEvents
도 포함되어 있습니다. 이 이벤트 핸들러가 없으면 실행 시간 동안 실행을 일시 중지 할 수 있습니다.
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