Excel Developer Tip


Return to The Spreadsheet Page

Excel page

Tip archives

Disabling a UserForm's Close Button

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.