我有由四个UserForm1组成的Checkboxes
CheckBox1 -->主复选框
CheckBox2 -->子复选框
CheckBox3 -->子复选框
CheckBox4 -->子复选框
现在,如果用户选中/取消检查CheckBox1,则使用此VBA自动检查/取消sub CheckBoxes 2-4:
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。
然后用户取消签入Checkbox2,而CheckBox1自动取消检查,而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发布于 2020-04-18 08:34:03
使用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 Subhttps://stackoverflow.com/questions/61286231
复制相似问题