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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,659
Messages
5,838,639
Members
430,558
Latest member
Krampus

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