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.
 

Some videos you may like

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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.
 

ajtilley

New Member
Joined
Jul 19, 2010
Messages
24
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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

Watch MrExcel Video

Forum statistics

Threads
1,126,888
Messages
5,621,430
Members
415,839
Latest member
Pollydooner

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
Top