如何在Excel中选择特定的表单控件复选框?

时间:2015-03-16 14:56:10

标签: excel vba excel-vba checkbox

我正在使用Excel 2010。

我在工作表中设置了10个表单控件复选框,我想自动选择一组复选框。我在此主题上看到的所有帖子都包括选中所有复选框或取消选中所有复选框。

以下是取消选中所有复选框的帖子中的VBA代码:

Sub clearcheck() 
Dim sh As Worksheet For Each sh In Sheets 
On Error Resume Next 
sh.CheckBoxes.Value = False 
On Error GoTo 0 Next sh 
End Sub

以下是基于此链的更新代码,但它仍然遇到语法问题以及未识别的子代码:

Sub highengagedonline() 
Dim cb As CheckBox, sht As Worksheet 
Set sht = Worksheets("Graph") 
For Each cb In sht.CheckBoxes 
    If cb.Name = "Check Box 35" or _
       cb.Name = "Check Box 36" or _
       cb.Name = "Check Box 37" or _
       cb.Name = "Check Box 38" or _
       cb.Name = "Check Box 39" Then
           cb.Value = 1 
    Else: cb.Value = 0 
    End If 
Next cb 
End Sub 

1 个答案:

答案 0 :(得分:1)

尝试以下方面的内容:

Sub ClearCheck()
    Dim cb As CheckBox, sht As Worksheet

    Set sht = Worksheets("Sheet1")

    For Each cb In sht.CheckBoxes
        If cb.Name = "Check Box 1" Then  
            cb.Value = 1
        Else if cb.Name = "Check Box 2" Then
            cb.Value = 0
        End If
    Next cb
End Sub