In projects, a situation often arises where user confirmation is required before closing a form. This behavior can be handled by using the QueryClose event procedure, which is triggered just before the window closes. This procedure has two parameters. If the first parameter is set to -1, the closing does not occur; if it is set to 0, the window closes. The second parameter identifies the reason that caused the window to close.
For example, in the following code, when you try to close the UserForm, a dialog box appears with two buttons: Yes and No, requiring user confirmation. If the user clicks Yes, the window closes; if No, it does not close.
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Select Case MsgBox("Close the window?", vbYesNo + vbQuestion)
Case vbYes: Cancel = 0
Case vbNo: Cancel = -1
End Select
End Sub