用户窗体关闭事件

UserForm Close Event

我有一个用户窗体,它在条件存在时循环打开和关闭。用户可以单击执行操作的多个按钮。问题是用户的不可预测性。其中一个问题是,用户没有单击其中一个按钮,而是单击用户窗体顶部的关闭 window 按钮,这会在不执行任何操作的情况下继续循环。

--- 编辑---
该按钮是否有一个事件,我可以用它来执行代码,以便我可以让它执行与表单本身上的取消按钮相同的操作。我不需要隐藏或禁用它本身。

例如,您可以将下面的宏添加到用户窗体代码模块中:

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

您可以只聚焦一个按钮,而不是 MsgBox:

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = vbFormControlMenu Then
        Cancel = True
        Me.Close_Button.SetFocus
    End If
End Sub

已编辑: 我发现了一个更好的选择:

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

    ' YOUR CODE HERE (Just copy whatever the close button does)

    If CloseMode = vbFormControlMenu Then
        Cancel = False
    End If

End Sub