如何在Excel中的组框中找到所选的形状?

时间:2013-05-07 04:54:22

标签: excel excel-vba vba

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

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

3 个答案:

答案 0 :(得分: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并重新运行之前无效。

enter image description here

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

答案 1 :(得分:1)

这似乎是一个有效的解决方案。

(致Dim ... As OptionButton致Kazjaw。这似乎是让.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

答案 2 :(得分:0)

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

Options

检查&#34; on&#34;使用方法:

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

对于大量选项&#34; FormControlType&#34;属性可以使用:

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