如何closures空白Excel工作簿以及macros工作簿?

我有2个Excel工作簿一个包含macros和另一个工作簿调用macros工作簿。

在主工作簿打开事件macros工作簿将在同一个应用程序实例中打开,当工作簿closures时,我正在closuresmacros工作簿,之后,我写了Appication.Quit,但这里问题是在closuresmacros工作簿之后,一个空白excel remians打开。

如何通过VBAclosures空白工作簿?

顺便说一下,我只在2013年在Office 2007和2010中面对这个问题,没有空白的Excel。

以下是Macro.xlsmModule1的代码

 Public Sub Test() MsgBox "Test" End Sub Public Sub Auto_Close() ThisWorkbook.Saved = True End Sub 

以下是Test.xlsm - Thisworkbook的代码

 Private Sub Workbook_BeforeClose(Cancel As Boolean) Application.Workbooks("Macro.xlsm").Close Module1.CodeFileClose End Sub Private Sub Workbook_Open() Module1.CodeFileOpen End Sub 

以下是Test.xlsm - Module1的代码

 Public Sub CodeFileOpen() Application.Workbooks.Open ("C:\Macro.xlsm") Application.Run "Macro.xlsm!Test" End Sub Public Sub CodeFileClose() MsgBox "Before Close" Application.Quit End Sub 

 Application.Quit 

, 要么

 thisworkbook.close 

将再次触发您的workbook_beforeclose事件!

所以你会循环它

在test.xlsm中,这个工作簿部分:

 Private Sub Workbook_BeforeClose(Cancel As Boolean) 'if you get in here, workbook is already going to close on error resume next 'if macro.xlsm not opened it would cause an error thisworkbook.saved= true ' (=no save) or false(=save) or delete line if you want to be asked with Application .displayalerts=false 'set to true if want to be asked before saving .enableevents=false 'block events from both workbooks .Workbooks("Macro.xlsm").Close .enableevents=true 'enable events from both workbooks end with 'Module1.CodeFileClose 'will cause looping (except if you add application.enableevents=false, but then it will be the case for all excell and you wont be able to triger events, or reset it to true by code) End Sub 

从我的理解中,您想要closures您的macros工作簿以及最初打开工作簿的工作簿。

如果这是正确的,为什么不尝试在你的CodeFileClose()Sub下面的代码:

 Public Sub CodeFileClose() MsgBox "Before Close" Workbooks("Macro.xlsm").Close End Sub 

如果我错了,请给出一些关于你的问题的更多细节。

这是我如何做到的。 诀窍是通过Application.Parent.Quitclosures应用程序的父节点:

 Sub Close_the_whole_excel_window() Dim New_session_Excel as New Excel.Application New_session_Excel.Workbooks.Open Filename:=(Path&FileName), ReadOnly:=True New_session_Excel.Quit ' This will close the workbook and leave an extra Excel window that stays behind New_session_Excel.Parent.Quit ' This will close the extra Excel window that stays behind End Sub