[英]how to check if closing the excel (workbook) vba
我在寻找一个可以返回一个代码boolean
0
, 1
或true
或false
,所以,如果有人通过点击X关闭Excel中它可以识别
Open the VBA Editor, then go on ThisWorkbook
(in the project tree on the left-side of the IDE) and you will access the code of the workbook events. 打开VBA编辑器,然后转到
ThisWorkbook
(在IDE左侧的项目树中),您将访问工作簿事件的代码。
Then, from the dropdown list on the right-top select the event BeforeClose
after having selected the Workbook
object in the left-top dropdown menu, and you will get the following code on the module: 然后,在左上角的下拉菜单中选择
Workbook
对象后,从右上角的下拉列表中选择事件BeforeClose
,您将在模块上获得以下代码:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
End Sub
That macro will be called every time someone tries to close the Workbook. 每当有人尝试关闭工作簿时,都会调用该宏。 There, you can put a global variable to control the action, something like:
在那里,您可以放置一个全局变量来控制操作,例如:
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.