【问题标题】:Uncheck CheckBox1 if one of multiple other Chechboxes are unchecked如果未选中多个其他复选框之一,请取消选中复选框
【发布时间】:2020-04-18 22:25:33
【问题描述】:

我有UserForm1,由四个Checkboxes组成:

CheckBox1 --> 主复选框
CheckBox2 --> 子复选框
CheckBox3 --> 子复选框
CheckBox4 --> 子复选框

现在,如果用户选中/取消选中 CheckBox1sub CheckBoxes 2-4 会使用此 VBA 自动选中/取消选中:

Private Sub CheckBox1_Change()
If CheckBox1.Value = True Then
    CheckBox2.Value = True
    CheckBox3.Value = True
    CheckBox4.Value = True
Else
    CheckBox2.Value = False
    CheckBox3.Value = False
    CheckBox4.Value = False
End If
End Sub

这一切都很完美。


但是,现在我想实现这一点,以防sub CheckBoxes 2-4 之一被取消选中,main checkbox 也会自动取消选中,而不会取消选中另一个sub CheckBoxes.

示例:
用户首先单击CheckBox1,然后自动检查Checkboxes 2-4
然后用户取消选中Checkbox2CheckBox1 自动取消选中,而Checkbox 3-4 保持选中状态。

类似这样的:

Sub Check_Uncheck()
If any of Checkbox 2-4 is unchecked then
only uncheck CheckBox1 but do not change any other sub CheckBox
End

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    使用UserForm 作用域变量让更改事件处理或不处理其代码:

    Option Explicit
    
    Dim blockChange As Boolean
    
    Private Sub CheckBox1_Change()
        If Not blockChange Then
            blockChange = True
            If CheckBox1 Then
                CheckBox2.Value = True
                CheckBox3.Value = True
                CheckBox4.Value = True
            Else
                CheckBox2.Value = False
                CheckBox3.Value = False
                CheckBox4.Value = False
            End If
            blockChange = False
        End If
    End Sub
    
    Private Sub CheckBox2_Click()
        OnOff
    End Sub
    
    Private Sub CheckBox3_Click()
        OnOff
    End Sub
    
    Private Sub CheckBox4_Click()
        OnOff
    End Sub
    
    
    Private Sub OnOff()
        If Not blockChange Then
            blockChange = True
            CheckBox1.Value = CheckBox2 And CheckBox3 And CheckBox4
            blockChange = False
        End If
    End Sub
    

    【讨论】:

    • 谢谢。正是我需要的。
    猜你喜欢
    • 2017-01-03
    • 1970-01-01
    • 2021-08-09
    • 2023-04-05
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    相关资源
    最近更新 更多