确定用户何时单击x-VBA

Jos*_*son 1 excel vba excel-vba

我想以编程方式决定用户在使用Visual Basic中的表单时是否单击了右上角的"x"按钮.

我试过了:

Private Sub nameOfForm_Exit()
     'code goes here
End Sub
Run Code Online (Sandbox Code Playgroud)

这是不成功的.任何帮助将非常感激.

cyb*_*shu 5

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    MsgBox "Good bye"

    '/ To prevent user from closing the form
    '/ Set cancel to True

    Cancel = True

    MsgBox "You can't close me!"
End Sub
Run Code Online (Sandbox Code Playgroud)