Excel 2010 复选框宏 - 如何在取消选择另一个框时取消选择 "Select All" 复选框
Excel 2010 Check Boxes Macro - How to deselect a "Select All" checkbox when another box is deselected
我有一个国家列表和 excel 模型的场景列表。每个列表都有带有 "All Countries" 和 "All Scenarios" 选项的复选框,这将 select 与每个列表相关的所有复选框。当我单击 "All Countries" 复选框时,每个国家/地区的复选框都会正确 selected。 "All Scenarios" 复选框也是如此。但是,如果我取消 select 单个 country/scenario,则所有 Countries/Scenarios 框保持选中状态。
当一个子复选框被取消select时,如何让"All Countries"和"All Scenarios"复选框被取消select?
下面是我目前的代码。
Sub SelectAll_Read()
Dim CB As CheckBox
For Each CB In ActiveSheet.CheckBoxes
If CB.Name <> ActiveSheet.CheckBoxes("MCB.1").Name And CB.Name <> ActiveSheet.CheckBoxes("MCB.2").Name Then
If Mid(CB.Name, 5, 1) = "1" Then
CB.Value = ActiveSheet.CheckBoxes("MCB.1").Value
ElseIf Mid(CB.Name, 5, 1) = "2" Then
CB.Value = ActiveSheet.CheckBoxes("MCB.2").Value
End If
End If
Next CB
End Sub
Sub Mixed_ReadState()
Dim CB As CheckBox
Dim i As String
For Each CB In ActiveSheet.CheckBoxes
i = Mid(CB.Name, 5, 1)
If CB.Name <> ActiveSheet.CheckBoxes("MCB" & i).Name And CB.Value <> ActiveSheet.CheckBoxes("MCB" & i).Value And ActiveSheet.CheckBoxes("MCB" & i).Value <> 2 Then
ActiveSheet.CheckBoxes("MCB" & i).Value = 2
Exit For
Else
ActiveSheet.CheckBoxes("MCB" & i).Value = CB.Value
End If
Next CB
End Sub
如果您将此分配给 sheet 上的所有复选框,那么它应该处理主<>子状态的同步。
Sub HandleClick()
Dim sht As Worksheet, nm As String
Dim arrA, arrB, masterClicked As Boolean
Dim c As CheckBox, cb As CheckBox
Dim mixedChildValues As Boolean
Set sht = ActiveSheet
nm = Application.Caller
Set cb = sht.CheckBoxes(nm) 'the clicked checkbox
arrA = Split(nm, ".") 'split the cb name on "." to give an array
masterClicked = (UBound(arrA) = 1) '"master" cb clicked?
mixedChildValues = False
'loop over all checkboxes on the sheet
For Each c In sht.CheckBoxes
'ignore the clicked checkbox
If c.Name <> nm Then
arrB = Split(c.Name, ".") 'split the name...
If arrA(1) = arrB(1) Then 'only consider same "family" of checkboxes
If masterClicked Then
'set all childern to "master" value if master was clicked
c.Value = cb.Value
Else
If (UBound(arrB) = 2) Then 'only look at children...
If c.Value <> cb.Value Then
mixedChildValues = True
Exit For 'no need to check further...
End If
End If 'child checkbox
End If
End If 'same family
End If 'not the clicked checkbox
Next c
'child checkbox was clicked - see if the master needs adjusting...
If Not masterClicked Then
sht.CheckBoxes(arrA(0) & "." & arrA(1)).Value = IIf(mixedChildValues, -4146, cb.Value)
End If
End Sub
我有一个国家列表和 excel 模型的场景列表。每个列表都有带有 "All Countries" 和 "All Scenarios" 选项的复选框,这将 select 与每个列表相关的所有复选框。当我单击 "All Countries" 复选框时,每个国家/地区的复选框都会正确 selected。 "All Scenarios" 复选框也是如此。但是,如果我取消 select 单个 country/scenario,则所有 Countries/Scenarios 框保持选中状态。
当一个子复选框被取消select时,如何让"All Countries"和"All Scenarios"复选框被取消select?
下面是我目前的代码。
Sub SelectAll_Read()
Dim CB As CheckBox
For Each CB In ActiveSheet.CheckBoxes
If CB.Name <> ActiveSheet.CheckBoxes("MCB.1").Name And CB.Name <> ActiveSheet.CheckBoxes("MCB.2").Name Then
If Mid(CB.Name, 5, 1) = "1" Then
CB.Value = ActiveSheet.CheckBoxes("MCB.1").Value
ElseIf Mid(CB.Name, 5, 1) = "2" Then
CB.Value = ActiveSheet.CheckBoxes("MCB.2").Value
End If
End If
Next CB
End Sub
Sub Mixed_ReadState()
Dim CB As CheckBox
Dim i As String
For Each CB In ActiveSheet.CheckBoxes
i = Mid(CB.Name, 5, 1)
If CB.Name <> ActiveSheet.CheckBoxes("MCB" & i).Name And CB.Value <> ActiveSheet.CheckBoxes("MCB" & i).Value And ActiveSheet.CheckBoxes("MCB" & i).Value <> 2 Then
ActiveSheet.CheckBoxes("MCB" & i).Value = 2
Exit For
Else
ActiveSheet.CheckBoxes("MCB" & i).Value = CB.Value
End If
Next CB
End Sub
如果您将此分配给 sheet 上的所有复选框,那么它应该处理主<>子状态的同步。
Sub HandleClick()
Dim sht As Worksheet, nm As String
Dim arrA, arrB, masterClicked As Boolean
Dim c As CheckBox, cb As CheckBox
Dim mixedChildValues As Boolean
Set sht = ActiveSheet
nm = Application.Caller
Set cb = sht.CheckBoxes(nm) 'the clicked checkbox
arrA = Split(nm, ".") 'split the cb name on "." to give an array
masterClicked = (UBound(arrA) = 1) '"master" cb clicked?
mixedChildValues = False
'loop over all checkboxes on the sheet
For Each c In sht.CheckBoxes
'ignore the clicked checkbox
If c.Name <> nm Then
arrB = Split(c.Name, ".") 'split the name...
If arrA(1) = arrB(1) Then 'only consider same "family" of checkboxes
If masterClicked Then
'set all childern to "master" value if master was clicked
c.Value = cb.Value
Else
If (UBound(arrB) = 2) Then 'only look at children...
If c.Value <> cb.Value Then
mixedChildValues = True
Exit For 'no need to check further...
End If
End If 'child checkbox
End If
End If 'same family
End If 'not the clicked checkbox
Next c
'child checkbox was clicked - see if the master needs adjusting...
If Not masterClicked Then
sht.CheckBoxes(arrA(0) & "." & arrA(1)).Value = IIf(mixedChildValues, -4146, cb.Value)
End If
End Sub