disable xl close

john tempest

Board Regular
Joined
Nov 20, 2005
Messages
54
i would like to disable the close button in xl and close xl from a userform
is this possible
regards john tempest
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

DominicB

Well-known Member
Joined
Oct 3, 2005
Messages
1,569
Good morning john tempest

Two parter this one. To stop Excel being closed via the red X (or File > Exit) use this code in the ThisWorkbook module :

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = True
MsgBox "Cancel this via the userform"
End Sub

Then this code attached to (in this instance) a button on a UserForm to close Excel ;

Code:
Private Sub CommandButton1_Click()
Application.EnableEvents = False
Application.Quit
End Sub

HTH

DominicB
 
L

Legacy 98055

Guest
Create a workbook property named "AllowClose" or something to that effect. The workbook simply will not allow closing unless this property equals True. Make it so from the userform. See the example...

ALLOWCLOSE_FROMUSERFORM.zip

<table width="100%" border="1" bgcolor="White" style="filter:progid:DXImageTransform.Microsoft.Gradient(endColorstr='#C0CFE2', startColorstr='#FFFFFF', gradientType='0');"><tr><TD><font size="2" face=Courier New>  <font color="#008000">'in thisworkbook</font>
  <font color="#0000A0">Private</font> pAllowClose <font color="#0000A0">As</font> <font color="#0000A0">Boolean</font>

  <font color="#0000A0">Friend</font> <font color="#0000A0">Property</font> <font color="#0000A0">Get</font> AllowClose() <font color="#0000A0">As</font> <font color="#0000A0">Boolean</font>
       AllowClose = pAllowClose
  <font color="#0000A0">End</font> <font color="#0000A0">Property</font>

  <font color="#0000A0">Friend</font> <font color="#0000A0">Property</font> <font color="#0000A0">Let</font> AllowClose(ByVal vNewValue <font color="#0000A0">As</font> Boolean)
       pAllowClose = vNewValue
  <font color="#0000A0">End</font> <font color="#0000A0">Property</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Workbook_Open()
       pAllowClose = <font color="#0000A0">False</font>
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> Workbook_BeforeClose(Cancel <font color="#0000A0">As</font> Boolean)
       Cancel = <font color="#0000A0">Not</font> pAllowClose
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>

  <font color="#008000">'is userform</font>
  <font color="#0000A0">Private</font> <font color="#0000A0">Sub</font> CommandButton1_Click()
       Unload Me
       ThisWorkbook.AllowClose = <font color="#0000A0">True</font>
      <font color="#008000"> 'you should determine if other workbooks are open before</font>
      <font color="#008000"> 'simply closing the entire application</font>
       Application.Quit
  <font color="#0000A0">End</font> <font color="#0000A0">Sub</font>
</FONT></td></tr></table><button onclick='document.all("9720064714328").value=document.all("9720064714328").value.replace(/<br \/>\s\s/g,"");document.all("9720064714328").value=document.all("9720064714328").value.replace(/<br \/>/g,"");window.clipboardData.setData("Text",document.all("9720064714328").value);'>Copy to Clipboard</BUTTON><textarea style="position:absolute;visibility:hidden" name="9720064714328" wrap="virtual">
'in thisworkbook
Private pAllowClose As Boolean

Friend Property Get AllowClose() As Boolean
AllowClose = pAllowClose
End Property

Friend Property Let AllowClose(ByVal vNewValue As Boolean)
pAllowClose = vNewValue
End Property

Private Sub Workbook_Open()
pAllowClose = False
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = Not pAllowClose
End Sub

'is userform
Private Sub CommandButton1_Click()
Unload Me
ThisWorkbook.AllowClose = True
'you should determine if other workbooks are open before
'simply closing the entire application
Application.Quit
End Sub</textarea>
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try:

Code:
' General module
Public CloseEnabled As Boolean

' ThisWorkbook module
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Not CloseEnabled Then
        MsgBox "Please use the form to close this workbook"
        Cancel = True
    End If
End Sub

' UserForm module
Private Sub CommandButton1_Click()
    CloseEnabled = True
    ThisWorkbook.Close
End Sub
 
Solution

Forum statistics

Threads
1,170,931
Messages
5,872,774
Members
432,944
Latest member
mj02

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