ActiveX 单选按钮 Excel VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14502518/
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
ActiveX Radio Buttons Excel VBA
提问by Naji Ahmed
On my main excel page I have two ActiveX control radio buttons called OptionButton1 and OptionButton2 I would like to use for a Yes/No option.
在我的主要 excel 页面上,我有两个名为 OptionButton1 和 OptionButton2 的 ActiveX 控件单选按钮,我想用于是/否选项。
In my code I have the following:
在我的代码中,我有以下内容:
If OptionButton1.Value = True Then
MsgBox "Yes."
ElseIf OptionButton2.Value = True Then
MsgBox "No."
End If
When I try to run my macro I get this error:
当我尝试运行我的宏时,出现此错误:
Runtime error '424':
Object Required
How do I fix this?
我该如何解决?
回答by Vinny Roe
You probably need to reference where the option buttons are, i.e. on which sheet:
您可能需要参考选项按钮的位置,即在哪个工作表上:
If Sheet1.OptionButton1.Value Then ...
This is because (I assume) your code is not on that sheet, so un-referenced objects will be assumed to be on the currentsheet, which may not be the right one.
这是因为(我假设)您的代码不在该工作表上,因此将假定未引用的对象在当前工作表上,这可能不是正确的工作表。
回答by Naji Ahmed
you may use the form controls option buttons instead and your code may look like
您可以改用表单控件选项按钮,您的代码可能看起来像
Dim Shp1 As Shape
Dim Shp2 As Shape
Dim Res As Integer 'Retuns the result
On Error Resume Next
Set Shp1 = Worksheets("sheet1").Shapes("Option Button 1")
Set Shp2 = Worksheets("sheet1").Shapes("Option Button 2")
If Shp1 Is Nothing Then
If Shp2 Is Nothing Then
'MsgBox "none" 'in case they were deleted off the sheet
Res = -3 '2 options deleted
ElseIf Shp2.ControlFormat.Value = xlOn Then
'MsgBox "second"
Res = 2
Else
'MsgBox "Only Button 2 exists and it is off"
Res = -1 'Option Button1 deleted
End If
Else
If Shp1.ControlFormat.Value = xlOn Then
'MsgBox "first"
Res = 1
Else
If Shp2 Is Nothing Then
'MsgBox "Only Button 1 exists and it is off"
Res = -2 'Option Button2 deleted
ElseIf Shp2.ControlFormat.Value = xlOn Then
'MsgBox "sec"
Res = 2
Else
'MsgBox "Both exists, both are off"
Res = 0
End If
End If
End If
return Res