• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

disable x to close in excel sheet

narsing rao

Member
Hi all,

i have excel sheet in that i have created form control button which save & close the excel.

but few of my user not using the button but using X close at top right corner close option.

i want my user to using this button so that in one instance they can save and close the excel , i want to disable this X option from excel.
 
Hi,
I think you want something like this.
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
        Cancel = True
        MsgBox "The X is disabled, please use the Close Command Button.", vbCritical
    End If
End Sub
 
it doesnt worked .. i have created form control button named save & close to save and close excel sheet in one instance. it is created in excel sheet only there is no userforms used.i have attached the pic and assigned below code to button

Code:
Sub SaveAndClose()
ActiveWorkbook.Save
ActiveWorkbook.Close
    'ActiveWorkbook.Close SaveChanges:=True
    Workbooks("Break_tr1.xlsm").Close
Application.Quit
End Sub
[\code]
 

Attachments

  • sheet.JPG
    sheet.JPG
    49.2 KB · Views: 7
I tought that you had a userform
i have created form
Try this instead in ThisWorbook
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If CloseMode = 0 Then Cancel = True
MsgBox "The X is disabled,please use the Save & Close button"
End Sub
 
Hi Thanks for the code ..
now the X disabled but when i click on Save & close button still its giving me the same message "The X is disabled,please use the Save & Close button"
 
Try this in the button code
Code:
Sub SaveAndClose()
If CloseMode = 0 Then Cancel = False
'rest of your code
end sub
 
Hi ..still i am geting same message " The X is disabled,please use the Save & Close button" and after clicking save & close button it shows same message and excel doesn't close
 
Hi, this works, tested this time
Code:
Public myRao As Boolean
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Run from "ThisWorkbook" module!
If myRao = True Then Exit Sub
Application.DisplayAlerts = False
Cancel = True
MsgBox "The X is disabled,please use the Save & Close button", vbCritical, "Error"
End Sub
Sub SaveAndClose()
'Run from "ThisWorkbook" module!
myRao = True
ActiveWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = False
ActiveWindow.Close False
End Sub
 
i am doing anything wrong...still i am getting same error ""The X is disabled,please use the Save & Close button" while clicking ion button

Code:
Public myRao AsBoolean
PrivateSub Workbook_BeforeClose(Cancel AsBoolean)
'Run from "ThisWorkbook" module!If myRao = TrueThenExitSub
Application.DisplayAlerts = False
Cancel = True
MsgBox "The X is disabled,please use the Save & Close button", vbCritical, "Error"
EndSub


i copied this in thisworkbook

and remaining code in code Module.
 
All the code goes in ThisWorkbook module
see attached (working example)
 

Attachments

  • sluiten zonder X in sheet.xlsb
    18.9 KB · Views: 38
Back
Top