Excel VBA:如何清除复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12711349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:58:07  来源:igfitidea点击:

Excel VBA: How to clear CheckBox

excelvbacheckbox

提问by user618677

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

I used the above to make sure that once the user clicks checkbox, he cannot uncheck it. However, I would want to be able to have a Button that my user could user to reset . everything and not only the checkbox. I have the below but it is not working . Could someone help me get it to work?

我使用上面的方法来确保一旦用户单击复选框,他就无法取消选中它。但是,我希望能够拥有一个我的用户可以重置的 Button 。一切,而不仅仅是复选框。我有以下但它不工作。有人可以帮我让它工作吗?

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

回答by Daniel

To solve the problem, you can replace your CheckBox9_Click sub with this:

为了解决这个问题,你可以用这个替换你的 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

The only difference is your Elsestatement is replaced with an Elseif. It will now only occur if the Caption <> "Incomplete",which is fine because the initial if will be true if the checkbox is clicked.

唯一的区别是您的Else语句被替换为Elseif. 它现在只会在 if Caption <> "Incomplete",which 没问题时发生,因为如果单击复选框,初始 if 将为 true。