MrExcel Publishing
Your One Stop for Excel Tips & Solutions

VBA UserForm question

Posted by Geoff on June 18, 2001 7:39 AM

When I create a userform using VBA in Excel, it automatically puts the X to close the window in the corner of the form. I would like to disable this option so that the user MUST click a button instead of being able to click the X. Does anyone know how to do this?

Thanks in advance,


Posted by Jerid on June 18, 2001 7:54 AM

Hello Geoff

You can put this code in your form, it will prevent the user from clicking the X. There are ways to remove the X through Windows API calls, but this is the easy way.

Private Sub Userform_QueryClose(iCancel As Integer, iCloseMode As Integer)

'Prevents the user from using the X in the upper right side of the activewindow to exit
If iCloseMode = vbFormControlMenu Then
MsgBox "Please use the Cancel button to quit.", vbCritical
iCancel = True
End If
End Sub


Posted by Jim on June 18, 2001 8:10 AM

J-Walks solution:
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

Posted by Geoff on June 18, 2001 8:22 AM

Awesome. Thanks a lot guys :)