ERLANDSEN DATA CONSULTING Excel & VBA Tips   Informasjon på norsk / Information in Norwegian

These pages are no longer updated and are only available for archive purposes.

Click here to visit the pages with updated information.

Prevent a UserForm from closing when the user clicks the x-button

If you want to prevent a Userform from closing when the user clicks the x-button in the top right corner of the dialog, you can add the macro below to the UserForms code module:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        Cancel = True
        MsgBox "You can't close the dialog like this!"
    End If
End Sub

This will also prevent the dialog to be closed when the user presses Alt+F4 on the keyboard.

 

Document last updated 2000-02-05 12:47:20      Printerfriendly version

User comments:
Ole P. from Norway wrote (2006-11-03 16:27:47 CET):
Re: Wonderful!
Take a look at the examples on this page.
S.Subramanian from Salem, India wrote (2006-11-03 13:28:03 CET):
Wonderful!
Thanks. By the same token can you give us a method by which we may prevent the user from using ^Break to stop the program execution?
Orlando Quares from Puerto Ordaz, Venezuela wrote (2005-05-02 14:13:05 CET):
Prevent a UserForm from closing when the user clicks the x-button
Thanks. By the way, your page in very useful. It will be my favorite when search excel macros.Congratulations.

 

Erlandsen Data Consulting     http://www.erlandsendata.no/   
Excel & VBA Tips   Copyright ©1999-2017    Ole P. Erlandsen   All rights reserved
E-mail Contact Address