简体   繁体   English

VBA Excel复选框以选择工作表上的所有某些特定复选框

[英]VBA Excel CheckBox to select all of some specific Checkboxes on Sheet

I have an Excel Sheet with two columns of CheckBoxes of which the first CheckBox of each is the "Master" Checkbox which toggles all other CheckBoxes. 我有一个包含两列CheckBox的Excel工作表,每列的第一个CheckBox是切换所有其他CheckBox的“ Master”复选框。
I got the code from This Tutorial . 我从本教程中获得了代码。

It worked fine until i copied the Code into the second Column. 在我将代码复制到第二列之前,它一直运行良好。
When Activating the first or the Second "Master" CheckBox it activates ALL CheckBoxes. 激活第一个或第二个“主”复选框时,它会激活所有复选框。
The First "Master" CheckBox is called "MCB1" the Second is in a Copy of this Code (with another Sub Name) and is Called MCB2. 第一个“主”复选框称为“ MCB1”,第二个在此代码的副本中(带有另一个子名称),称为MCB2。

Here's my Code: 这是我的代码:

Sub SelectAll_Read()
Dim CB As CheckBox
 For Each CB In ActiveSheet.CheckBoxes
  If CB.Name <> ActiveSheet.CheckBoxes("MCB1").Name Then
   CB.Value = ActiveSheet.CheckBoxes("MCB1").Value
  End If
 Next CB
End Sub

Sub Mixed_ReadState()
Dim CB As CheckBox
For Each CB In ActiveSheet.CheckBoxes
  If CB.Name <> ActiveSheet.CheckBoxes("MCB1").Name And CB.Value <> ActiveSheet.CheckBoxes("MCB1").Value And ActiveSheet.CheckBoxes("MCB1").Value <> 2 Then
    ActiveSheet.CheckBoxes("MCB1").Value = 2
Exit For
   Else
     ActiveSheet.CheckBoxes("MCB1").Value = CB.Value
  End If
Next CB
End Sub

First of all you need to differenciate Checkboxes from Column 1 to those in Column 2. 首先,您需要区分第1列和第2列中的复选框。

For instance you could name follower checkboxes in Column 1 as MCB1.1 , MCB1.2 , MCB1.3 and so on. 例如,您可以在第1列MCB1.1跟随者复选框命名为MCB1.1MCB1.2MCB1.3等。 The same thing for checkboxes in column 2: MCB2.1 , MCB2.2 , MCB2.3 ... 第2列中的复选框的MCB2.1相同: MCB2.1MCB2.2MCB2.3 ...

"Master" checkboxes in column 1 and 2 could be named MCB1 and MCB2 . 列1和2中的“主”复选框可以命名为MCB1MCB2

Then your code needs to be modified as follows: (Didn't have time to test it) 然后,您的代码需要进行如下修改:(没有时间对其进行测试)

Sub SelectAll_Read()
Dim CB As CheckBox
    For Each CB In ActiveSheet.CheckBoxes
        If CB.Name <> ActiveSheet.CheckBoxes("MCB1").Name And CB.Name <> ActiveSheet.CheckBoxes("MCB2").Name Then
            If Mid(CB.Name, 4, 1) = "1"
                CB.Value = ActiveSheet.CheckBoxes("MCB1").Value
            ElseIf Mid(CB.Name, 4, 1) = "2"
                CB.Value = ActiveSheet.CheckBoxes("MCB2").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, 4, 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

This alternative method worked for me 这种替代方法对我有用

Sub SelectAll_CHECK_BOX()
    Dim CB As CheckBox
    Dim CB1 As Range
        Set CB1 = ActiveSheet.Range("A1:A30")
    For Each CB In ActiveSheet.CheckBoxes
            If Not Intersect(CB.TopLeftCell, CB1) Is Nothing Then
                CB.Value = ActiveSheet.CheckBoxes("MCB1").Value
          End If
    Next CB
    Dim CB2 As Range
        Set CB2 = ActiveSheet.Range("B1:B30")
    For Each CB In ActiveSheet.CheckBoxes
            If Not Intersect(CB.TopLeftCell, CB2) Is Nothing Then
                CB.Value = ActiveSheet.CheckBoxes("MCB2").Value
            End If
    Next CB
End Sub

Sub Mixed_ReadState()
Dim CB As CheckBox
    For Each CB In ActiveSheet.CheckBoxes
        If CB.Name <> ActiveSheet.CheckBoxes("MCB1").Name And CB.Value <> ActiveSheet.CheckBoxes("MCB1").Value And ActiveSheet.CheckBoxes("MCB1").Value <> 2 Then
        ActiveSheet.CheckBoxes("MCB1").Value = 2
        Exit For
        Else
        ActiveSheet.CheckBoxes("MCB1").Value = CB.Value
        End If
    Next CB
End Sub` 



Sub Mixed_ReadState()
Dim CB As CheckBox
    For Each CB In ActiveSheet.CheckBoxes
        If CB.Name <> ActiveSheet.CheckBoxes("MCB2").Name And CB.Value <> ActiveSheet.CheckBoxes("MCB2").Value And ActiveSheet.CheckBoxes("MCB2").Value <> 2 Then
        ActiveSheet.CheckBoxes("MCB2").Value = 2
        Exit For
        Else
        ActiveSheet.CheckBoxes("MCB2").Value = CB.Value
        End If
    Next CB
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM