excel vba - 检查是否选择了单选按钮?

时间:2011-10-02 04:33:56

标签: excel vba scripting

bt

我正在尝试检查这些简单的单选按钮组的值,但我的语法已关闭,是否有人知道要更改的内容? 注意:它们是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

2 个答案:

答案 0 :(得分:7)

试试这个

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

答案 1 :(得分:0)

我有类似的问题。为了解决这个问题,我决定使用带有此宏VBA代码的切换按钮来访问其值,并相应地切换我的显示:

Call ThisWorkbook.Sheets("MySheet").toggleDisplay(CBool(ThisWorkbook.Sheets("MySheet").ToggleButton1.Value))