Bypassing the BeforeClose Event

Written by Allen Wyatt (last updated August 20, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

If Roy holds down the Shift key when opening a workbook, Excel bypasses the running of any "auto" macros that would normally run, including the Workbook_Open event. He wonders if there is an equally easy way, when closing a workbook, to cause Excel to bypass the Workbook_BeforeClose event.

There is not an equally easy way. That doesn't mean it can't be done, but "equally easy" generally means "something built in; some key to press or setting to make." Such keys and settings to bypass the BeforeClose event just aren't built into Excel.

What you can do is adjust how you program the BeforeClose event so that if a particular condition is met, then the code within the event handler is bypassed. An easy approach is to simply bypass the code if a particular worksheet cell is selected, in this manner:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If ActiveCell.Address = "$Z$27" Then Exit Sub
    '
    ' Your normal code here...
    '
End Sub

In this case, if the active cell—the one selected when the BeforeClose event is triggered—happens to be cell Z27, then your regular code in the event handler is skipped. If any other cell is selected, then it isn't skipped. (I purposely picked cell Z27 because it is both esoteric and obscure. You can, of course, change the code to reflect any other cell you desire.)

If you are dead-set on checking to see if the Shift key is being held down, you can do that by utilizing the Windows API. This involves adding some code to the declarations area of a module. This can be a regular module; it doesn't have to be in the ThisWorkbook code window. You just need to make sure it is in the declarations area, before any procedures are declared.

#If VBA7 Then
    Declare PtrSafe Function GetKeyState Lib "USER32" (ByVal vKey As Long) As Integer
#Else
    Declare Function GetKeyState Lib "USER32" (ByVal vKey As Long) As Integer
#End If

Note that the #If VBA7 conditional directive is used so that if you are using Excel 2010 or later, the correct syntax of the declaration will be used so there is no errors on either 32-bit or 64-bit versions of the program.

Now, within your BeforeClose event handler you can do the following:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Const VSHIFT = &H10
    Const SHIFTED = &H80

    If GetKeyState(VSHIFT) And SHIFTED Then Exit Sub
    '
    ' Your normal code here...
    '
End Sub

If the Shift key is held down as the BeforeClose event is triggered, then the BeforeClose code is skipped. You should note that it may be a bit difficult to figure out when to hold down the Shift key or if you've held it down long enough. For this reason, I tend to prefer the "check the ActiveCell" approach better, as you can simply select the cell, close the workbook, and forget about it.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2628) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Viewing Footnotes and Endnotes

Footnotes and endnotes are normally visible with the rest of your document, but such visibility is dependent on which ...

Discover More

Searching for Borders

Want to find all the paragraphs in your document that have borders applied to them? The regular Find and Replace tool ...

Discover More

Adjusting Mouse Click Sensitivity

Mouse not working as you expect? Here are a few things that may get things back to the way they should be.

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Using Macros in Protected Workbooks

Having problems with using macros in a protected workbook? There could be any number of causes (and solutions) as ...

Discover More

Item Not Available in Library

When sharing workbooks with others, you may find that the macros in those workbooks may not work as you expect. This tip ...

Discover More

Making Sure Cells are Filled In before Saving

When creating a workbook that will be used by others, you may wish to ensure that the user fills in some cells before ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 8?

2022-08-20 11:26:29

J. Woolley

With the Tip's Declare statement for GetKeyState, My Excel Toolbox includes the following procedure to return the True/False (Down/Up) status of Shift, Ctrl, and Alt:

Const VK_SHIFT As Integer = &H10 ' Shift
Const VK_CONTROL As Integer = &H11 ' Ctrl
Const VK_MENU As Integer = &H12 ' Alt

Sub GetKeys_ShiftCtrlAlt(ByRef Shift As Boolean, _
    ByRef Ctrl As Boolean, ByRef Alt As Boolean)
    Shift = (GetKeyState(VK_SHIFT) < 0)
    Ctrl = (GetKeyState(VK_CONTROL) < 0)
    Alt = (GetKeyState(VK_MENU) < 0)
End Sub

See https://sites.google.com/view/MyExcelToolbox/


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.