Excel 2010 VBA 提示并取消选中复选框单击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8977365/
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
Excel 2010 VBA Prompt and Uncheck on Checkbox Click
提问by Josh McDonald
I have a columns of checkboxes in Excel. When one of them is checked, I want to prompt the user "Are you sure?" or something to that effect. If they answer "Yes", move on and do nothing. If they answer no, I want to uncheck the box (the one they just clicked). It seems simple enough, but I can't for some reason get it to work.
我在 Excel 中有一列复选框。当其中之一被选中时,我想提示用户“你确定吗?” 或类似的东西。如果他们回答“是”,继续前进,什么也不做。如果他们回答“否”,我想取消选中该框(他们刚刚单击的那个)。这看起来很简单,但由于某种原因我无法让它工作。
I've tried something like:
我试过这样的事情:
r = MsgBox("Are you sure", vbYesNo, "Eh?")
If r <> vbYes Then Application.Undo
But that doesn't quite work. It seems so simple, yet it ends up being a pain.
但这并不完全有效。看似简单,结果却很痛苦。
采纳答案by mischab1
The problem is that checking a checkbox doesn't trigger an Excel action that can be undone. (Doesn't matter if the controls are Form or ActiveX.)
问题是选中复选框不会触发可以撤消的 Excel 操作。(不管控件是 Form 还是 ActiveX。)
If your checkboxes areActiveX controls then you can do this:
如果您的复选框是ActiveX 控件,那么您可以这样做:
Private Sub VerifyCheckBox(chk As CheckBox)
If chk.Value Then ' only ask question if checkbox has been checked
If MsgBox("Are you sure", vbYesNo, "Eh?") <> vbYes Then
chk.Value = Not chk.Value
End If
End If
End Sub
You would then call that in each checkbox's click event.
然后,您将在每个复选框的点击事件中调用它。
Private Sub CheckBox1_Click()
VerifyCheckBox CheckBox1
End Sub
Private Sub CheckBox2_Click()
VerifyCheckBox CheckBox2
End Sub