Disabling A Userform’s Close Button

Category: UserForms | [Item URL]

Every UserForm that you create contains a Close button ("x") in its title bar. Clicking the Close button (or pressing Alt+F4) closes the UserForm.

In some cases, you may not want to allow the user to close the UserForm in this manner. There's no easy way to disable the Close button, but a few lines of VBA code will prevent it from being used.

Activate the code module for your UserForm, and enter the following procedure:

Private Sub UserForm_QueryClose _
  (Cancel As Integer, CloseMode As Integer)
'   Prevents use of the Close button
    If CloseMode = vbFormControlMenu Then
        MsgBox "Clicking the Close button does not work."
        Cancel = True
    End If
End Sub

When the user clicks the Close button, a message box appears and the UserForm remains open.


Search for Tips


All Tips

Browse Tips by Category

Tip Books

Needs tips? Here are two books, with nothing but tips:

Contains more than 100 useful tips and tricks for Excel 2013 | Other Excel 2013 books | Amazon link: 101 Excel 2013 Tips, Tricks & Timesavers

Contains more than 200 useful tips and tricks for Excel 2007 | Other Excel 2007 books | Amazon link: John Walkenbach's Favorite Excel 2007 Tips & Tricks

© Copyright 2016, J-Walk & Associates, Inc.
Privacy Policy