【问题标题】:Change Checkbox Form Controls in a group更改组中的复选框表单控件
【发布时间】:2020-10-04 10:36:28
【问题描述】:

有六个表单复选框已“分组”并命名为 app_list。

整个工作表

中有单独的表单复选框

当我运行我在网上找到的这个宏时:

Sub ClearFormsCheckboxes()
Dim chBox As CheckBox
For Each chBox In ActiveSheet.CheckBoxes
    If chBox.Value = 1 Then
        chBox.Value = 0
    End If
Next
End Sub

...单个复选框未选中,但组“app_list”保持不变。我一直在考虑只做一个 ungroup - uncheck - regroup 方法,但这似乎太多了。

我必须做些什么来改变整个团队的价值。

【问题讨论】:

    标签: excel vba checkbox


    【解决方案1】:

    先说几点:

    1. 始终使用Option Explicit。它强制声明变量并避免错误
    2. 避免使用ActiveSheet。而是明确参考工作表
    3. 当您将几个checkboxes 分组时,您基本上会创建一个shape。该组的成员属于.GroupItems 集合,他们也是shapes

    以下应该可以解决问题:

    Option Explicit
    
    Sub ClearFormsCheckboxes()
    
    Dim chBox As CheckBox
    Dim groupedChBox As Shape
    Dim sht As Worksheet
    Set sht = ThisWorkbook.Worksheets("Sheet1") 'Use the name of the sheet where the checkboxes are located
    
    For Each chBox In sht.CheckBoxes 'non grouped checkboxes
        If chBox.Value = 1 Then
            chBox.Value = 0
        End If
    Next chBox
    
    For Each groupedChBox In sht.Shapes("Group 1").GroupItems 'Grouped checkboxes. Use the name of the group
        If groupedChBox.ControlFormat.Value = 1 Then
            groupedChBox.ControlFormat.Value = 0
        End If
    Next groupedChBox
    
    End Sub
    

    【讨论】:

    • 谢谢斯塔夫罗斯。特别是对于添加的指针。真的有帮助!
    猜你喜欢
    • 2019-07-22
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 2022-01-19
    • 2014-07-07
    • 1970-01-01
    • 2018-03-29
    相关资源
    最近更新 更多