简体   繁体   中英

how to check if closing the excel (workbook) vba

我在寻找一个可以返回一个代码boolean 01truefalse ,所以,如果有人通过点击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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM