如何在 Excel 用户窗体中对已选择的 OptionButton 触发点击事件

How to fire click event on already-selected OptionButton in Excel Userform

我在某处读到,一旦在用户窗体上选择了选项按钮(或单选按钮),就不会注册单击事件。这是真的?

如果是这样,除了使用复选框之外,还有什么好的解决方法?

谢谢!

更新:根据 user3561813 的建议,我使用以下代码尝试了 MouseUp 事件:

Private Sub myOptionButton_MouseUp() MyUserForm.Show End Sub

我收到这个编译错误:"Procedure declaration does not match description of event or procedure having the same name"

更新 2:此代码不允许取消选择已选择的单选按钮:

Private Sub myOptionButton_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) If myOptionButton.Value = True Then myOptionButton.Value = False Else myOptionButton.Value = True End If End Sub

奇怪的是,如果我单步执行代码,它确实有效。但是如果我只是 运行 它通过了,它不会。

您可以使用 MouseUp 事件来 运行 您喜欢的任何代码。

可能有点晚了,但是对于 Update 2,您是否应该使用

Controls(myOptionButton).Value = False

更改值,因此,

If myOptionButton.Value = True Then
    Controls(myOptionButton).Value =  False
Else
    Controls(myOptionButton).Value = True
End If