Ask Your Question

Revision history [back]

One way to verify if a userform has been closed using the "X" button in Windows is to use the UserForm_QueryClose event. This event is triggered when the user clicks the "X" button, and can be used to perform actions before the form is closed or to cancel the closing of the form.

Here's an example code snippet that shows how to use the UserForm_QueryClose event to verify if a userform has been closed using the "X" button:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        ' This means the user clicked the "X" button
        ' Perform actions to verify if the form should be closed or not
        ' e.g. prompt the user to save changes or cancel the close operation
    End If
End Sub

In this example, the CloseMode argument is checked to see if it has a value of vbFormControlMenu, which indicates that the user clicked the "X" button. If this is the case, the code can perform additional checks or prompt the user to save changes before allowing the form to be closed.