Issue : I am encountering the issue that if I try to close a workbook from an other one, the code stops running.
This issue has been exposed in a few posts already but there doesn't seem to have a solution.
What I am trying to do :
I have a code in workbook WB1.xlsm called "Code1" which opens WB2 the following way
Workbooks.Open("WB2.xlsm").RunAutoMacros(xlAutoOpen)
The Auto_Open code form WB2 is now running, I do my things in WB2 (copy data) and then the goal will be to paste those data in WB3 and close WB1 so I proceed as the following at the end of the Auto_Open code from WB2 before jumping into WB3:
For Each wbk In Workbooks If wbk.Name = "WB1.xlsm" Then 'wbk.Activate 'DoEvents 'ActiveWorkbook.Close False wbk.Close False 'Application.OnTime Now + TimeValue("00:00:01"), "wbk.Close" End If Next Workbooks.Open(PathTo_WB3).RunAutoMacros (xlAutoOpen)
The problem is that the code stops after closing WB1 ( above you can see different attempts to close it but they all fail...)
Question : Is it actually possible to close a workbook from an other one without seeing the code being stopped ?
wbk.Close
is inside a loop that iterates all workbooks in theApplicationWorkbooks
collection, and that includes the workbook containing the code that's currently executing that loop (i.e.ThisWorkbook
) - closing the file that contains the executing code will stop that code, that's just how it is. So please edit to clarify exactly what you mean, we're not looking at your screen and we can't read minds. – Mathieu GuindonAuto_Open
macro that closes other workbooks is a very very bad idea. – Mathieu Guindon