【问题标题】:Checking Value of Checkbox and setting value for all other Checkboxes accordingly检查复选框的值并相应地设置所有其他复选框的值
【发布时间】:2017-08-18 09:39:09
【问题描述】:

我正在尝试创建类似“Mastercheckbox”的东西,它会自动检查我工作表上的所有其他复选框。因此,我试图在单击“Mastercheckbox”后立即启动以下 VBA 代码,我正在使用表单工具而不是 Active X 创建我的复选框,以确保我的 VBA 与任何机器兼容(我读过Active X 可能会导致一些问题)并得到以下代码:

Sub Mastercheckbox()

Dim Chk As CheckBox

With ActiveSheet.Shapes("Mastercheckbox")
    If .ControlFormat.Value = False Or .ControlFormat.Value = True Then
        If .ControlFormat.Value = False Then
            For Each chk In ActiveSheet.CheckBoxes
                If Not chk.Name = "Mastercheckbox" Then
                chk.Value = False
                End If
            Next chk
        End If

        If Not .ControlFormat.Value = True Then
            For Each chk In ActiveSheet.CheckBoxes
                If Not chk.Name = "Mastercheckbox" Then
                chk.Value = True
                End If
            Next chk
        End If
     Else: MsgBox ("couldn't check value of mastercheckbox")
     End If
End With
End Sub

For 循环中的所有内容都有效,但检查 Mastercheckbox 的值由于某种原因无法正常工作,它会直接跳转到 Else 案例。谁能帮帮我?

【问题讨论】:

    标签: vba excel checkbox


    【解决方案1】:

    不是ActiveX 控件,您必须对照xlOn(对应于1)和xlOff `(对应于-4146)检查其值

    你也可以像下面这样重构你的代码:

    Option Explicit
    
    Sub Mastercheckbox()        
        With ActiveSheet.Shapes("Mastercheckbox")
            If .ControlFormat.Value = xlNone Then
                MsgBox ("couldn't check value of mastercheckbox")
            Else
                HandleCheckBoxes .ControlFormat.Value
            End If
        End With
    End Sub
    
    Sub HandleCheckBoxes(val As Long)
        Dim Chk As CheckBox
    
        For Each Chk In ActiveSheet.CheckBoxes
            If Not Chk.name = "Mastercheckbox" Then Chk.Value = val
        Next Chk
    End Sub
    

    【讨论】:

    • 不客气。查看我的编辑以稍微缩短代码
    【解决方案2】:

    由于您决定使用User_Form 复选框,您需要将变量定义为Shape。为了检查它们的值,您需要检查它们是xlOn 还是xlOff

    为了查看您在按下“Mastercheckbox”后读取的值,您可以使用以下代码:

    Dim MasterCB As Shape
    
    Set MasterCB = ActiveSheet.Shapes("Mastercheckbox") '<-- set the MasterCB  variable
    
    If MasterCB.ControlFormat.Value = xlOn Then
        MsgBox "Is checked"
    ElseIf MasterCB.ControlFormat.Value = xlOff Then
        MsgBox "Not checked"
    End If
    

    帖子代码

    Option Explicit
    
    Sub Mastercheckbox()
    
    Dim Chk As Shape
    Dim MasterCB As Shape
    
    ' set the MasterCB  variable
    Set MasterCB = ActiveSheet.Shapes("Mastercheckbox")
    
    ' loop through all shapes in ActiveSheet
    For Each Chk In ActiveSheet.Shapes
        If Chk.Type = msoFormControl Then '<-- check your shape type is User_Form type
            If Chk.FormControlType = xlCheckBox Then '<-- check if the shape is a checkbox
                If Chk.Name <> "Mastercheckbox" Then 
                    Chk.ControlFormat.Value = MasterCB.ControlFormat.Value '<-- modify all checkboxes to the value of "Mastercheckbox"
                End If
            End If
        End If
    Next Chk
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多