disable the X to close

ajtilley

New Member
Joined
Jul 19, 2010
Messages
24
Hi

Can anyone help, is there a VBA code which i can put into an excel 2007 workbook which disables the x button on the top right hand corner and forces the user to use a macro enabled button to close.

I ask because at present i have disabled save option on close, but i want to make sure people dont accidentally close the file without saving.

Thanks for any help.
 

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.
In the ThisWorkbook module

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = True
End Sub

Then in your code to close the file you will need to include

Code:
Application.EnableEvents = False

to prevent the event code from running.
 
Upvote 0
I put this code in, however it does not seem to make a difference, if a person presses the x to close, it still closes, naturally without saving. I cant see why it isnt working.
I have tried these variations of the workbook code:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
     Cancel = "True"
End Sub
 
Private Sub Workbook_BeforeClose(Cancel As Boolean)
     Me.Saved = "True"
End Sub

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
     Me.Saved = "True"
     Cancel = "True
End Sub


The Macro code for save and quit was as follows:

Code:
Sub saveclose()
'
' saveclose Macro
'
'
    Application.EnableEvents = False
    ActiveWorkbook.Save
    ActiveWorkbook.Close
End Sub
 
Upvote 0
The code that I posted has to go in the ThisWorkbook module. In the VBE, in the Project window double click ThisWorkbook and paste in the code.

You should also turn events back on

Code:
Sub saveclose()
'
' saveclose Macro
'
'
    Application.EnableEvents = False
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,219,034
Messages
6,145,893
Members
450,654
Latest member
lorento

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