Disable X to close Excel

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to find a way to close a Workbook through a Macro and prevent closing with the standard X in the corner of the application.

I have this so far;

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cancel = False Then
Call MsgBox("You must use the Close button on the menu to exit this file", vbCritical, "Button disabled")
Cancel = True
Exit Sub
End If
End Sub

This works fine, but it also triggers when I use the command I have put on the ribbon which is this;

Code:
Application.DisplayAlerts = False
If Workbooks.Count = 1 Then
Application.Quit
ActiveWorkbook.Close SaveChanges:=True
Else
ActiveWorkbook.Close SaveChanges:=True
End If
Application.DisplayAlerts = True

What I need to achieve is to force the user to only be able to close the file using the button I've put on the ribbon and not the X in the corner.

Is there such a way?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
try this in your workbook module:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Cancel = Not bClose
    If Not bClose Then MsgBox "You must use the Close button on the menu to exit this file", vbCritical, "Button disabled"
End Sub


Private Sub Workbook_Open()
    bClose = False
End Sub



and in a normal module:

Code:
Public bClose As Boolean


Sub closeWorkbook()
bClose = True
ActiveWorkbook.Close True
End Sub
 
Upvote 0
Hi - thanks but this doesn't work - I still get the warning when I try to close the workbook through my ribbon button.
 
Upvote 0
the sub linked to your button is

Code:
Sub closeWorkbook()
bClose = True
ActiveWorkbook.Close True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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