Shortcut To Temporary Enable Previously Disabled Workbook Close Function [X]

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code that I use to disable the close function of the workbook close button [X]. (thank you ZVI).
Can anyone suggest a way that I can create a keyboard shortcut that I could use to temporarily enable the feature? It's implemented for shared user protection, but is awkward for me when testing the workbook. If I could just have a shortcut to enable, rather than going intot he code and commenting everything out would be handy.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi,
Put this code: to Thisworkbook module:
VBA Code:
Private Sub Workbook_Open()
  Application.OnKey "%{UP}", "EnableXExit"    ' Alt+Up
  Application.OnKey "%{DOWN}", "DisableXExit" ' Alt_Down
End Sub

The below code goes to standard module:
VBA Code:
Sub EnableXExit()
  IsOnExit = True
  'Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",True)"
  MsgBox "XExit is enabled"
End Sub

Sub DisableXExit()
  IsOnExit = False
  'Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",False)"
  MsgBox "XExit is disabled"
End Sub

To disable shortcuts before this workbook closing, insert these 2 code lines into Sub front_exit below the line Application.ScreenUpdating = True
VBA Code:
    Application.OnKey "%{UP}", ""
    Application.OnKey "%{DOWN}", ""

Select & run Workbook_Open.
Then use Alt+Up and Alt+Down shortcuts

Note: code for the [EXIT] button still works and can modify IsOnExit variable as well.
 
Last edited:
Upvote 0
Hi Vladimir my friend. Thank you for sharing your solution.

Before I go about monkeying around with my code that is working well, (this is simply a convenience thing and not necessary for the end user experience), may I confirm...

I use this line
Code:
Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",True)"
in a procedure I use called "wkbk_on_open" that executes in my workbook open event. This line accompanies workbook feature formatting like hiding scrollbars, formula bars, scroll bars etc, and of course removing the ribbon. Even after executed the window still has the minimize, maximize and close buttons.

So, I anticipate (and my apologies if my assumptions are incorrect) that your shortcuts will simply reveal and conceal the taskbar (ribbon).

With the [X] button disabled in function in the workbook close event (that you so kindly helped figure out for me), I'm hoping for a shortcut I can use to bypass this limitation. (as the developer to bypass in the interim instead of having to always exit via the interface ... simply a time savings for me)
 
Upvote 0
Then the ExitX functionality is enabled via Alt+UP, the Excel reacts on clicking its [X] button as usual.

To hide/show ribbon and other interface things. look at post #2 onto commented lines:
VBA Code:
'Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",True/False)"
Just uncomment it to restore/hide ribbon via shortcuts, like this:
VBA Code:
Sub EnableXExit()
  IsOnExit = True
  Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",True)"
  ' put here the code to restore all hidden toolbars etc.
  ' ...
  MsgBox "XExit is enabled"
End Sub

Sub DisableXExit()
  IsOnExit = False
  Application.ExecuteExcel4Macro "show.toolbar(""Ribbon"",False)"
  ' put here the code to hide toolbars etc.
  ' ...
  MsgBox "XExit is disabled"
End Sub
Write your code to hide/restore those interface things just below the comment: ' put here the code ...
If it's not enough then please describe step-by-step your actions and what should happen.
 
Upvote 0
Solution
Ah, ok. My apologies, I missed the fact you had the hide/show ribbon line commented out.
Works like a charm Vladimir!! Thank you so much.
 
Upvote 0
That is good, please post back if something is still wrong.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top