Results 1 to 3 of 3
  1. #1
    Bronze Lounger
    Join Date
    Sep 2007
    Posts
    1,203
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want to add some code to the "X" on a userform. I want to maintain it's preset functionality (close) but also generate a messagebox. How can I do that?

    I thought that clicking the "X" in the vbe would generate a code click event, but no. ??

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Use the QueryClose event of the userform. The event procedure has 2 arguments:

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    Cancel is False by default. If you set it to True in the event procedure, you'll cancel closing the form.

    CloseMode can have one of the following values:

    vbFormControlMenu if the user clicked the x in the upper right corner.
    vbFormCode if you close the userform in code, for example if you placed a command button on the form that unloads it.
    vbAppWindows if Windows is shut down; Windows will then close all open applications.
    vbAppTaskManager if the user terminates Excel from the Task Manager.

    So you could use

    Code:
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
      If CloseMode = vbFormControlMenu Then
    	MsgBox "You're closing the userform!", vbInformation
      End If
    End Sub

  3. #3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •