阻止UserForm_Initialize触发ToggleButton_Click事件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阻止UserForm_Initialize触发ToggleButton_Click事件相关的知识,希望对你有一定的参考价值。

我有一张(“设置”),大部分时间都需要隐藏。我创建了Settings UserForm,其中包含各种设置按钮和切换按钮,隐藏/显示隐藏的工作表(然后单击并输入密码)。

用于隐藏/显示工作表的宏:

Private Sub SettingsTB_Click()

Dim strPassTry As String
Dim strPassword As String
Dim lTries As Long
Dim bSuccess As Boolean

If SettingsTB.Value = True Then
    strPassword = "asd"
    For lTries = 1 To 3
        strPassTry = InputBox("Enter password", "Show Settings sheet")
        If strPassTry = vbNullString Then Exit Sub
        bSuccess = strPassword = strPassTry
        If bSuccess = True Then Exit For
        MsgBox "Incorrect password"
    Next lTries

    If bSuccess = True Then
        Worksheets("Settings").Visible = True
    End If
Else
    Worksheets("Settings").Visible = xlSheetHidden
End If

End Sub

该宏按预期工作,出现问题然后我打开UserForm并且“设置”工作表保持可见。 UserForm_Initialize事件触发SettingsTB_Click事件(要求输入密码)。

UserForm_initialize中的代码用于记住切换按钮位置(没有它,每次打开UserForm,在FALSE中切换按钮):

Private Sub UserForm_Initialize()

If Worksheets("Settings").Visible = True Then
    SettingsTB.Value = True
Else
    SettingsTB.Value = False
    End If

End Sub

是否可以阻止SettingsTB_Click触发UserForm_Initialize,还是应该使用完全不同的方法?

答案

使用公共变量或tag-property来阻止click事件的运行。

Private Sub SettingsTB_Click()


Dim strPassTry As String
Dim strPassword As String
Dim lTries As Long
Dim bSuccess As Boolean

If SettingsTB.Tag Then Exit Sub

If SettingsTB.Value = True Then
    strPassword = "asd"
    For lTries = 1 To 3
        strPassTry = InputBox("Enter password", "Show Settings sheet")
        If strPassTry = vbNullString Then Exit Sub
        bSuccess = strPassword = strPassTry
        If bSuccess = True Then Exit For
        MsgBox "Incorrect password"
    Next lTries

    If bSuccess = True Then
        Worksheets("Settings").Visible = True
    End If
Else
    Worksheets("Settings").Visible = xlSheetHidden
End If

End Sub
Private Sub UserForm_Initialize()
SettingsTB.Tag = True
If Worksheets("Settings").Visible = True Then

    SettingsTB.Value = True
Else
    SettingsTB.Value = False
    End If
SettingsTB.Tag = False
End Sub

以上是关于阻止UserForm_Initialize触发ToggleButton_Click事件的主要内容,如果未能解决你的问题,请参考以下文章

如果超出现有数量,则触发以阻止订单更新

阻止所有滚动,直到函数触发

详解css3 pointer-events(阻止hoveractiveonclick等触发事件来

可以阻止 GitLab Webhook 在 Jenkins 中触发构建吗

如何阻止空格键触发聚焦的 QPushButton?

如何阻止:hover、:active等鼠标行为状态的触发?