excel vba - 检查是否选择了单选按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7624631/
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 vba - check if radio button is selected?
提问by toop
I'm trying to check the value of these simple radio button groups but my syntax is off, does anyone know what to change? Note: they are Excel Option Buttons not ActiveX ones and they are not on a userform.
我正在尝试检查这些简单单选按钮组的值,但我的语法已关闭,有人知道要更改什么吗?注意:它们是 Excel 选项按钮而不是 ActiveX 选项按钮,它们不在用户表单上。
If Worksheets("Input").Shapes("Option Button 3").Select.Value = xlOn Then
MsgBox "fir"
ElseIf Worksheets("Input").Shapes("Option Button 4").Select.Value = xlOn Then
MsgBox "sec"
Else
MsgBox "none" 'in case they were deleted off the sheet
End If
回答by chris neilsen
Try this
尝试这个
Sub ZX()
Dim shp3 As Shape
Dim shp4 As Shape
On Error Resume Next
Set shp3 = Worksheets("Input").Shapes("Option Button 3")
Set shp4 = Worksheets("Input").Shapes("Option Button 4")
On Error Goto 0
If shp3 Is Nothing Then
If shp4 Is Nothing Then
MsgBox "none" 'in case they were deleted off the sheet
ElseIf shp4.ControlFormat.Value = xlOn Then
MsgBox "sec"
Else
MsgBox "Only Button 4 exists and it is off"
End If
Else
If shp3.ControlFormat.Value = xlOn Then
MsgBox "fir"
Else
If shp4 Is Nothing Then
MsgBox "Only Button 3 exists and it is off"
ElseIf shp4.ControlFormat.Value = xlOn Then
MsgBox "sec"
Else
MsgBox "Both exists, both are off"
End If
End If
End If
End Sub
回答by Michael Fayad
I had a similar problem. To solve it, I decided to use a toggle button with this macro VBA code to access its value and toggle my display accordingly to it:
我有一个类似的问题。为了解决这个问题,我决定使用带有这个宏 VBA 代码的切换按钮来访问它的值并相应地切换我的显示:
Call ThisWorkbook.Sheets("MySheet").toggleDisplay(CBool(ThisWorkbook.Sheets("MySheet").ToggleButton1.Value))