Excel 更改组中的复选框窗体控件

Excel 更改组中的复选框窗体控件,excel,vba,checkbox,Excel,Vba,Checkbox,有六个表单复选框已“分组”并命名为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

整个工作表中有单独的表单复选框

运行联机找到的此宏时:

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
…单个复选框未选中,但组“应用程序列表”保持不变。我一直在考虑只做一个取消分组-取消选中-重新分组的方法,但这似乎太多了

我必须要做些什么才能改变整个组的值。

先说几句:

  • 始终使用
    选项Explicit
    。它使变量的声明成为强制性的,并且避免了错误
  • 避免使用
    ActiveSheet
    。而是显式地引用工作表
  • 当您将几个
    复选框分组时
    基本上创建了一个
    形状
    。该组的成员属于
    .GroupItems
    集合,它们也是
    形状
  • 下面应该可以做到这一点:

    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
    
    首先说几句话:

  • 始终使用
    选项Explicit
    。它使变量的声明成为强制性的,并且避免了错误
  • 避免使用
    ActiveSheet
    。而是显式地引用工作表
  • 当您将几个
    复选框分组时
    基本上创建了一个
    形状
    。该组的成员属于
    .GroupItems
    集合,它们也是
    形状
  • 下面应该可以做到这一点:

    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
    

    谢谢斯塔夫罗斯。特别是对于添加的指针。真的很有帮助!谢谢斯塔夫罗斯。特别是对于添加的指针。真的很有帮助!