Excel VBA:如何清除CheckBox

时间:2012-10-03 15:05:20

标签: excel vba checkbox

Private Sub CheckBox9_Click()
    If CheckBox9.Value = True Then
        CheckBox9.Caption = "Done"
        ActiveWorkbook.Sheets("Well Planning Checklist").Tab.ColorIndex = 4
        'ActiveSheet.Tab.ColorIndex = 22
        Range("Q17").Value = CheckBox9.Caption
    Else
        If LCase(Range("Q17").Value) = CheckBox9.Caption Then
            CheckBox9.Value = Not (CheckBox9.Value)
        Else
            CheckBox9.Value = Not (CheckBox9.Value)
        End If
    End If
End Sub

我使用上面的内容确保一旦用户点击复选框,他就无法取消选中它。但是,我希望能够有一个用户可以重置的按钮。 一切,而不仅仅是复选框。我有以下但它没有工作。有人可以帮助我开始工作吗?

Private Sub CommandButton1_Click()
    CheckBox9

    ActiveWorkbook.Sheets("Well Design Section").CheckBox9.Caption = "Incomplete "
    ActiveWorkbook.Sheets("Well Design Section").CheckBox9.Value = False
    Range("Q17").Value = "Incomplete"
End Sub

1 个答案:

答案 0 :(得分:2)

要解决此问题,您可以使用以下命令替换CheckBox9_Click子:

Private Sub CheckBox9_Click()
    If CheckBox9.Value = True Then
        CheckBox9.Caption = "Done"
        ActiveWorkbook.Sheets("Well Planning Checklist").Tab.ColorIndex = 4
        'ActiveSheet.Tab.ColorIndex = 22
        Range("Q17").Value = CheckBox9.Caption
    Elseif Checkbox9.Caption <> "Incomplete" Then
        If LCase(Range("Q17").Value) = CheckBox9.Caption Then
            CheckBox9.Value = Not (CheckBox9.Value)
        Else
            CheckBox9.Value = Not (CheckBox9.Value)
        End If
    End If
End Sub

唯一的区别是您的Else语句已替换为Elseif。它现在只有在Caption <> "Incomplete",时才会出现,因为如果单击该复选框,则初始if将为true。