在我的主要Excel页面上,我有两个名为OptionButton1和OptionButton2的ActiveX控件单选按钮,我想用于是/否选项。
在我的代码中,我有以下内容:
If OptionButton1.Value = True Then
MsgBox "Yes."
ElseIf OptionButton2.Value = True Then
MsgBox "No."
End If
当我尝试运行我的宏时,我收到此错误:
Runtime error '424':
Object Required
我该如何解决这个问题?
答案 0 :(得分:3)
您可能需要参考选项按钮的位置,即在哪张纸上:
If Sheet1.OptionButton1.Value Then ...
这是因为(我假设)您的代码不在该工作表上,因此将假定未引用的对象位于当前工作表上,这可能不是正确的。
答案 1 :(得分:0)
您可以使用表单控件选项按钮,代码可能看起来像
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