【问题标题】:How to check/uncheck all other checkboxes on spreadsheet and hide/unhide some columns?如何选中/取消选中电子表格上的所有其他复选框并隐藏/取消隐藏某些列?
【发布时间】:2023-03-31 04:15:01
【问题描述】:

工作表上有 6 个复选框。

第一部分:

每个复选框(第一个除外)用于隐藏/取消隐藏一列。

第二部分:

第一个复选框选中/取消选中其他复选框,选中时取消隐藏从 A 到 E 的列,取消选中时隐藏它们。

总结一下:

checkbox-1: check/uncheck other 5 checkboxes and unhide/hide columns A-E
checkbox-2: hide/unhide column A
checkbox-3: hide/unhide column B
checkbox-4: hide/unhide column C
checkbox-5: hide/unhide column D
checkbox-6: hide/unhide column E

单击复选框 2-6 时,我尝试使用以下代码隐藏/取消隐藏列。有效。但是我将如何做第二部分?

Sub CheckBox_Click()

    Dim vis As Boolean, ac As String, col As String

    ac = Application.Caller

    With ActiveSheet

        vis = (.Shapes(ac).ControlFormat.Value = 1)

        Select Case ac
            Case "checkbox-2": col = "A"
            Case "checkbox-3": col = "B"
            ...
        End Select

        If col <> "" Then .Columns(col).Hidden = vis

    End With

End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    将框 2-6 连接到 CheckTheBoxes 宏,将框 1 连接到 InvertTheBoxes 宏。

    InvertTheBoxes 循环遍历工作表形状。每个不是“框 1”的形状都将被选中/取消选中,并跟随 CheckTheBoxes 宏。如果工作表上有更多形状,则需要稍微更改代码。但这是未经修改的答案:

    Option Explicit
    
    'checkbox-1: check/uncheck other 5 checkboxes and unhide/hide columns A-E
    'checkbox-2: hide/unhide column A
    'checkbox-3: hide/unhide column B
    'checkbox-4: hide/unhide column C
    'checkbox-5: hide/unhide column D
    'checkbox-6: hide/unhide column E
    
    Dim Vis As Boolean, Col$, Shp As Shape
    
    Sub CheckTheBoxes(Optional ByVal AC$)
        ' If a checkbox runs this code, it will use Application.Caller,
        ' since the AC string would be emtpy.
        ' This way, you can run this macro from another macro (InvertTheBoxes), 
        ' provided you return the shape name (which that macro does).
        If AC = "" Then AC = Application.Caller
    
        With ActiveSheet
            Vis = (.Shapes(AC).ControlFormat.Value = 1)
    
            Select Case AC
                Case "checkbox-2": Col = "A"
                Case "checkbox-3": Col = "B"
                Case "checkbox-4": Col = "C"
                Case "checkbox-5": Col = "D"
                Case "checkbox-6": Col = "E"
            End Select
    
            If Col <> "" Then .Columns(Col).Hidden = Vis
    
        End With
    End Sub
    
    Sub InvertTheBoxes()
    Dim AC2$
        AC2 = Application.Caller
        With ActiveSheet
            For Each Shp In .Shapes
                If Shp.Name <> AC2 Then
                    ' Change the checkbox status (from checked to unchecked and vice versa)
                    If Shp.ControlFormat.Value = 1 Then
                        Shp.ControlFormat.Value = -4146 ' Unchecked
                    Else
                        Shp.ControlFormat.Value = 1 ' Checked
                    End If
                    CheckTheBoxes Shp.Name ' Call the hide/show procedure
                End If
            Next Shp
        End With
    End Sub
    

    如果您想遍历具有特定名称的形状,请尝试使用这个 InvertTheBoxes2 宏:

    Sub InvertTheBoxes2()
    Dim AC2$
        AC2 = Application.Caller
        With ActiveSheet
            For Each Shp In .Shapes
                ' Will only loop through shapes starting with "checkbox-"
                If Left(Shp.Name, 9) = "checkbox-" And Shp.Name <> AC2 Then
                    ' Change the checkbox status (from checked to unchecked and vice versa)
                    If Shp.ControlFormat.Value = 1 Then
                        Shp.ControlFormat.Value = -4146 ' Unchecked
                    Else
                        Shp.ControlFormat.Value = 1 ' Checked
                    End If
                    CheckTheBoxes Shp.Name ' Call the hide/show procedure
                End If
            Next Shp
        End With
    End Sub
    

    只需确保您的其他复选框具有不同的名称,即“cbo-1”、“cbo-2”等。或者尝试更彻底地命名它们。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 2019-04-06
      相关资源
      最近更新 更多