3

我有一个带有选项按钮的组框,我需要找出在 VBA 中选择了其中的哪一个。我已经浏览 MSDN 几个小时了,但找不到解决方案。

必须有一种方法可以找到选定的选项按钮。可能通过每个选项按钮按名称和 for-each 找到组?

4

3 回答 3

1

如果您确实需要检查分组的 OptionButton(以我们对任何类型的形状进行分组的方式分组),您可以使用以下代码:

Sub Grouped_into_UnitType()
    Dim i!
'grouped into 'UnitType' Shape
    For i = 1 To ActiveSheet.Shapes("UnitType").GroupItems.Count

        With ActiveSheet.Shapes("UnitType").GroupItems(i).ControlFormat
            If .Value = 1 Then
                MsgBox "Chosen item: " & i

            End If
        End With
    Next i

End Sub

编辑请记住下图,如果我们有选项按钮,则上面的代码将解决问题,这些选项按钮按照我们对放置在工作表中的任何形状进行分组的方式进行分组。

图片下的代码将找出选择了哪个选项按钮,如果它们位于 GroupBox 中。代码检查 OptionButton 所在组的名称。

重要的提示!在我关闭 Excel 并再次运行它之前,下面的代码不起作用。

在此处输入图像描述

Sub Grouped_into_GroupBox_UnitType()

    Dim OB As OptionButton


    For Each OB In ActiveSheet.OptionButtons

    'check if grouped into 'UnitType' Shape
        If OB.GroupBox.Name = "UnitType" Then

            If OB.Value = 1 Then
                MsgBox "Chosen item: " & OB.Name & _
                        vbNewLine & _
                        "Alt text: " & OB.ShapeRange.AlternativeText

            End If
        End If
    Next

End Sub
于 2013-05-07T05:58:26.303 回答
1

这似乎是一个可行的解决方案。

(向 KazJaw 点头Dim ... As OptionButton。这似乎是开始.GroupBox工作的关键)

Function WhichOption(shpGroupBox As Shape) As OptionButton
    Dim shp As OptionButton
    Dim shpOptionGB As GroupBox
    Dim gb As GroupBox

    If shpGroupBox.FormControlType <> xlGroupBox Then Exit Function
    Set gb = shpGroupBox.DrawingObject
    For Each shp In shpGroupBox.Parent.OptionButtons
        Set shpOptionGB = shp.GroupBox
        If Not shpOptionGB Is Nothing Then
            If shpOptionGB.Name = gb.Name Then
                If shp.Value = 1 Then
                    Set WhichOption = shp
                    Exit Function
                End If
            End If
        End If
    Next
End Function

像这样使用它

Sub test()
    Dim shpOpt As OptionButton

    Set shpOpt = WhichOption(Worksheets("Sheet1").Shapes("Group Box 1"))
    Debug.Print shpOpt.Name
End Sub
于 2013-05-07T08:29:58.543 回答
0

假设您有两个标准选项按钮:

选项

要检查其是否“开启”,请使用:

Dim opt As Shape
Set opt = Worksheets("Sheet1").Shapes("Option Button 1")

If opt.ControlFormat.Value = xlOn Then
    Debug.Print "option is ""on"" value of 1"
Else
    Debug.Print "option is ""off"" value of -4146"
End If

要获得其替代文本,请使用:

Debug.Print "Alternate Text is: " & opt.AlternativeText

对于大量选项,可以使用“FormControlType”属性:

Dim s as Shape
For Each s In Worksheets("Sheet1").Shapes

    If s.FormControlType = xlOptionButton Then

        If s.ControlFormat.Value = xlOn Then
            Debug.Print "option is ""on"" value of 1"
        Else
            Debug.Print "option is ""off"" value of -4146"
        End If

        Debug.Print "Alternate Text is: " & s.AlternativeText

    End If

Next

如果你想要一个特定的组:

Dim s As Shape, o

For Each s In Worksheets("Sheet1").Shapes

    If s.FormControlType = xlOptionButton Then

        Set o = s.OLEFormat.Object

        If o.GroupBox.Name = "Group Box 3" Then

            If s.ControlFormat.Value = xlOn Then
                Debug.Print "Option is ""on"" value of 1"
            Else
                Debug.Print "Option is ""off"" value of -4146"
            End If

            Debug.Print "Alternate Text is: " & s.AlternativeText
            Debug.Print "Group: " & o.GroupBox.Name
        End If

        Set o = Nothing
    End If

Next
于 2013-05-07T05:50:49.520 回答