如何检查是否关闭 excel(工作簿)vba

how to check if closing the excel (workbook) vba

我正在寻找可以 return boolean 01truefalse 的代码,所以它可以识别是否有人通过单击 X

关闭了 excel

打开VBA编辑器,然后继续ThisWorkbook(在IDE左侧的项目树中),您将访问工作簿事件的代码.

然后,在 select 编辑左上下拉菜单中的 Workbook 对象后,从右上角的下拉列表 select 事件 BeforeClose ,您将在模块上获得以下代码:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

End Sub

每次有人试图关闭工作簿时都会调用该宏。 在那里,您可以放置​​一个全局变量来控制操作,例如:

someoneIsClosing = True '<-- this is a globally defined variable
Cancel = True '<-- if you want to cancel the closing action
myMacro '<-- if you want to go back to "myMacro", i.e. any macro of your project and delegate it from handling the event.