【问题标题】:Check box to select and deselect all other check boxes in spreadsheet复选框以选中和取消选中电子表格中的所有其他复选框
【发布时间】:2013-03-09 04:49:51
【问题描述】:

我有一个电子表格,其中包含每个订单项的复选框。我想在顶部放置一个复选框,当它被选中/取消选中时,它将选中和取消选中电子表格中的所有其他复选框。以下是我到目前为止的代码,如果我的“复选框 1”被选中,它将选中所有复选框,但如果取消选中,它不会取消选中它们。如果取消选择“复选框 1”,我需要添加什么才能使框也取消选择。感谢您的帮助。

Sub SelectAllCheckBox()
Dim CB As CheckBox
If ActiveSheet.CheckBoxes("Check Box 1").Value Then
    For Each CB In ActiveSheet.CheckBoxes
        If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then
            CB.Value = True
        End If
    Next CB
End If
End Sub

我也遇到了另一个问题。我有一个宏来清除工作表,以便可以运行不同的宏。该宏中包含删除所有复选框的代码。我将如何措辞代码以使其不删除“复选框 1”。这是我的代码。

Sheets("Quote Sheet").Select
Range("D3:D7").Select
Selection.ClearContents
Rows("11:1000").Select
Selection.Delete Shift:=xlUp
ActiveSheet.CheckBoxes.Delete
Selection.FormatConditions.Delete

我尝试了以下方法,但没有成功。

Sheets("Quote Sheet").Select
Range("D3:D7").Select
Selection.ClearContents
Rows("11:1000").Select
Selection.Delete Shift:=xlUp
If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then    
ActiveSheet.CheckBoxes.Delete
Selection.FormatConditions.Delete

【问题讨论】:

    标签: excel vba


    【解决方案1】:
    Sub SelectAllCheckBox()
        Dim CB As CheckBox
    
        For Each CB In ActiveSheet.CheckBoxes
            If CB.Name <> ActiveSheet.CheckBoxes("Check Box 1").Name Then
                CB.Value = ActiveSheet.CheckBoxes("Check Box 1").Value
            End If
        Next CB
    
    End Sub
    

    第二部分:

    Dim CB as CheckBox, n as long, x as long
    
    n = ActiveSheet.CheckBoxes.Count
    
    For x = n to 1 Step -1        
        Set CB = ActiveSheet.CheckBoxes(x)
        If CB.Name <> "Check Box 1" Then CB.Delete
    
    Next x
    

    【讨论】:

    • 感谢蒂姆,这非常有效。我试过做类似的事情,但不知道如何正确表达。
    • 蒂姆,我在上面发布了另一个问题,如果您对此有一个快速的解决方案,我们将不胜感激。我觉得我走在正确的轨道上,我只是不知道该怎么说。
    猜你喜欢
    • 1970-01-01
    • 2013-11-04
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 2020-08-12
    相关资源
    最近更新 更多