如何关闭空白Excel工作簿以及Macro工作簿? [英] How to close blank excel workbook along with Macro workbook?

查看:140
本文介绍了如何关闭空白Excel工作簿以及Macro工作簿?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个Excel工作簿,一个包含宏,另一个工作簿称为宏工作簿.

I have 2 Excel workbooks one contains Macros and the other workbook calls Macro workbook.

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

In main workbook open event Macro workbook will be opened in the same application instance and when the workbook closes I am closing Macro workbook and after that I have written Appication.Quit, but here the problem is after closing Macro workbook one blank excel remians open.

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

How to close the blank workbook through VBA?

顺便说一句,仅在2013年我在Office 2007和2010中面临此问题的方式就没有空白.

By the way I am facing this issue in Office 2007 and 2010 only in 2013 there is no blank excel.

下面是 Macro.xlsm - Module1

Public Sub Test()
MsgBox "Test"
End Sub

Public Sub Auto_Close()
ThisWorkbook.Saved = True
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 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事件!!!!

will trigger again your workbook_beforeclose event !!!!

所以您将循环播放

test.xlsm中的本工作簿部分:

in test.xlsm , thisworkbook section :

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

这篇关于如何关闭空白Excel工作簿以及Macro工作簿?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆