0

在我的主 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

我该如何解决?

4

2 回答 2

3

您可能需要参考选项按钮的位置,即在哪个工作表上:

If Sheet1.OptionButton1.Value  Then ...

这是因为(我假设)您的代码不在该工作表上,因此将假定未引用的对象在当前工作表上,这可能不是正确的。

于 2013-01-24T13:35:01.937 回答
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
于 2013-04-15T14:13:10.433 回答