【问题标题】:How to close blank excel workbook along with Macro workbook?如何关闭空白 Excel 工作簿和宏工作簿?
【发布时间】:2014-08-01 18:42:10
【问题描述】:

我有 2 个 Excel 工作簿,一个包含宏,另一个工作簿调用宏工作簿。

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

如何通过VBA关闭空白工作簿?

顺便说一句,我在 Office 2007 和 2010 中遇到此问题,仅在 2013 年没有空白 excel。

下面是Macro.xlsm-Module1中的代码

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

【问题讨论】:

  • 测试这个。您打算手动关闭宏书还是应该通过宏测试例程来关闭它?
  • 我将只关闭 Test.xlsm 并且宏工作簿将通过 Sub 关闭

标签: vba excel


【解决方案1】:
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

【讨论】:

  • 在关闭 Test.xlsm 后遵循您的方法仍然空白 excel 左侧
【解决方案2】:

据我了解,您希望关闭宏工作簿以及最初打开工作簿的工作簿。

如果是这样,你为什么不在你的 CodeFileClose() Sub 中尝试以下代码:

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

如果我错了,请详细说明您的问题。

【讨论】:

  • 是的 Flouks,在上面的 scnario 中我可以关闭 Macro.xlasm 但之后一个空白的 excel 仍然打开,我不希望它仍然存在
【解决方案3】:

这就是我的做法。诀窍是使用 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

【讨论】:

    【解决方案4】:

    我通过先关闭父级然后关闭应用程序来使其工作:

    ActiveWorkbook.Save
    Application.DisplayAlerts = False
    
    Excel.Parent.Quit
    Application.Quit
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多