Disable X (Close) in form

N Prakash

Active Member
Joined
Nov 7, 2003
Messages
409
Hi! Friends,

I have a form where in I want to disable the X (Close). I have used following code in my macro

Application.CommandBars("File").Controls("Close").Enabled = False

It is not working. Can anybody help me please

Prakash
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi

I got the code

Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "You can't close the form like that."
Cancel = True
End If
End Sub

Where to put this code, whether in the form or in the module.

Regards,
Prakash
 
Upvote 0
N Prakash said:
Hi

I got the code

Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "You can't close the form like that."
Cancel = True
End If
End Sub

Where to put this code, whether in the form or in the module.

Regards,
Prakash
?The paragraph immediately above the code that you refer to explains where to place the code! ;)
... using a UserForm_QueryClose event procedure in the code module for the UserForm.
 
Upvote 0
Hi

I tried putting in the module sheet and also in the form. But it is not working. I do not know what mistake I am doing.


Prakash
 
Upvote 0
Hi Prakash,

If you double-click the userform, VBA editor will take you top the code window. If you then select 'Query Close' from the drop-down list to the top right of the code window, it will automatically insert the start & end of the sub as follows:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

End Sub
Paste your code into there (but exclude the 'Private Sub...' and 'End Sub' lines of course.
It might be a good idea to remove the code from anywhere else you may have put it.
 
Upvote 0
Hi! Alan,

I tried the procedure as you have explained. No doubt it dispalys the message box, but it will not disable the X button of my form. If I close the message box, then I can close the form by clicking on the X button. I have seen some of the message boxes and dialogue boxes where in the X button is disabled.

Regards,
Prakash
 
Upvote 0

Forum statistics

Threads
1,214,617
Messages
6,120,541
Members
448,970
Latest member
kennimack

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