运行 另一个 excel 中的宏,但在调用的宏完成后无法 return 到当前子
Run Macro in another excel, but fail to return to current sub after the called Macro completed
ParentExcel.xlsm 有 sub 在 ChildExcel1.xlsm 和 ChildExcel2.xlsm
中调用一个 sub
//in ParentExcel.xlsm
Sub ParentSub()
Set runWorkbook = Workbooks.Open(Filename:=mChildPath)
Application.Run runWorkbookName & "!" & ChildSub1
/*Edit*/Set runWorkbook2 = Workbooks.Open(Filename:=mChildPath2)
Application.Run runWorkbookName2 & "!" & ChildSub2
End sub
//in ChildExcel1.xlsm
Sub ChildSub1()
MsgBox "Called ChildSub1"
/*Added*/ThisWorkbook.Close
End sub
//in ChildExcel2.xlsm
Sub ChildSub2()
MsgBox "Called ChildSub2"
/*Added*/ThisWorkbook.Close
End sub
我的问题是,程序在 ChildSub1() 完成后停止并且 ChildExcel1.xlsm 关闭。然后我无法执行 ParentSub()
中的剩余代码
为了从已关闭的工作簿调用子程序,请使用下一种方式。使用他们的全名将使调用打开必要的工作簿。如果它们已经打开,代码也可以工作:
Sub ParentSub()
dim mChildPath as string, mChildPath2 as string
mChildPath = "full name of the workbook" 'please fill here the real path
mChildPath2 = "full name of the second workbook" 'the same as above
Application.Run "'" & mChildPath & "'!" & "ChildSub1"
Application.Run "'" & mChildPath2 & "'!" & "ChildSub2"
End sub
现在,进行调用时,必要的工作簿将打开,并由调用的 Sub
...
关闭
如果您关闭代码所在的工作簿 (runWorkbook),则执行将停止(VBA 是单线程的,使用 ChildSub1 中的关闭语句,运行时假定您已完成)。
您需要将 close 语句放入调用例程中 - 无论如何这样更干净,打开工作簿的例程也会关闭它。
请注意,您使用的 runWorkbookName
既未定义也未在您向我们展示的代码中设置。您可以改用以下代码:
Set runWorkbook = Workbooks.Open(Filename:=mChildPath)
Application.Run runWorkbook.Name & "!" & ChildSub1
runWorkbook.Close
Set runWorkbook2 = Workbooks.Open(Filename:=mChildPath2)
Application.Run runWorkboo2.kName & "!" & ChildSub2
runWorkbook2.Close
ParentExcel.xlsm 有 sub 在 ChildExcel1.xlsm 和 ChildExcel2.xlsm
中调用一个 sub//in ParentExcel.xlsm
Sub ParentSub()
Set runWorkbook = Workbooks.Open(Filename:=mChildPath)
Application.Run runWorkbookName & "!" & ChildSub1
/*Edit*/Set runWorkbook2 = Workbooks.Open(Filename:=mChildPath2)
Application.Run runWorkbookName2 & "!" & ChildSub2
End sub
//in ChildExcel1.xlsm
Sub ChildSub1()
MsgBox "Called ChildSub1"
/*Added*/ThisWorkbook.Close
End sub
//in ChildExcel2.xlsm
Sub ChildSub2()
MsgBox "Called ChildSub2"
/*Added*/ThisWorkbook.Close
End sub
我的问题是,程序在 ChildSub1() 完成后停止并且 ChildExcel1.xlsm 关闭。然后我无法执行 ParentSub()
中的剩余代码为了从已关闭的工作簿调用子程序,请使用下一种方式。使用他们的全名将使调用打开必要的工作簿。如果它们已经打开,代码也可以工作:
Sub ParentSub()
dim mChildPath as string, mChildPath2 as string
mChildPath = "full name of the workbook" 'please fill here the real path
mChildPath2 = "full name of the second workbook" 'the same as above
Application.Run "'" & mChildPath & "'!" & "ChildSub1"
Application.Run "'" & mChildPath2 & "'!" & "ChildSub2"
End sub
现在,进行调用时,必要的工作簿将打开,并由调用的 Sub
...
如果您关闭代码所在的工作簿 (runWorkbook),则执行将停止(VBA 是单线程的,使用 ChildSub1 中的关闭语句,运行时假定您已完成)。
您需要将 close 语句放入调用例程中 - 无论如何这样更干净,打开工作簿的例程也会关闭它。
请注意,您使用的 runWorkbookName
既未定义也未在您向我们展示的代码中设置。您可以改用以下代码:
Set runWorkbook = Workbooks.Open(Filename:=mChildPath)
Application.Run runWorkbook.Name & "!" & ChildSub1
runWorkbook.Close
Set runWorkbook2 = Workbooks.Open(Filename:=mChildPath2)
Application.Run runWorkboo2.kName & "!" & ChildSub2
runWorkbook2.Close