Vba 如何关闭空白excel工作簿和宏工作簿?
我有两个Excel工作簿,一个包含宏,另一个工作簿调用宏工作簿 在主工作簿打开事件中,宏工作簿将在同一个应用程序实例中打开,当工作簿关闭时,我正在关闭宏工作簿,然后我就编写了应用程序。退出,但问题是在关闭宏工作簿后,一个空白excel将被打开 如何通过VBA?< /P>关闭空白工作簿? 顺便说一下,我在2007年和2010年的Office中遇到了这个问题,只是2013年没有空白excel 下面是Vba 如何关闭空白excel工作簿和宏工作簿?,vba,excel,Vba,Excel,我有两个Excel工作簿,一个包含宏,另一个工作簿调用宏工作簿 在主工作簿打开事件中,宏工作簿将在同一个应用程序实例中打开,当工作簿关闭时,我正在关闭宏工作簿,然后我就编写了应用程序。退出,但问题是在关闭宏工作簿后,一个空白excel将被打开 如何通过VBA?< /P>关闭空白工作簿? 顺便说一下,我在2007年和2010年的Office中遇到了这个问题,只是2013年没有空白excel 下面是Macro.xlsm-Module1 Public Sub Test() MsgBox "Test"
Macro.xlsm
-Module1
Public Sub Test()
MsgBox "Test"
End Sub
Public Sub Auto_Close()
ThisWorkbook.Saved = True
End Sub
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
以下是Test.xlsm-本工作簿中的代码
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 Test()
MsgBox "Test"
End Sub
Public Sub Auto_Close()
ThisWorkbook.Saved = True
End Sub
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
据我所知,您希望关闭宏工作簿以及最初打开工作簿的工作簿
如果这是正确的,为什么不在CodeFileClose()子代码中尝试以下代码:
Public Sub CodeFileClose()
MsgBox "Before Close"
Workbooks("Macro.xlsm").Close
End Sub
如果我错了,请就你的问题提供更多的细节
Application.Quit
,或
将在关闭事件之前再次触发您的工作簿
所以你将循环它
在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
我是这样做的。诀窍是使用application.parent.Quit关闭应用程序的父级:
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
我先关闭家长,然后关闭应用程序,使其正常工作:
ActiveWorkbook.Save
Application.DisplayAlerts = False
Excel.Parent.Quit
Application.Quit
测试这个。您打算手动关闭宏工作簿还是宏测试例程应该关闭它?我将仅关闭Test.xlsm,宏工作簿将通过SubYes Flouks关闭,在上面的scnario中,我可以关闭Macro.xlasm,但在关闭Test.xlsm后,一个空白的excel仍保持打开状态,我不想让它保持打开状态。按照您的方法,关闭Test.xlsm后,仍然保留空白的excel