如何在Excel中查找选定的组合框中的形状?

3

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

一定有一种方法可以找到选定的选项按钮。可能是通过名称查找分组,并逐个遍历每个选项按钮?


1
@glh,我认为这更像是一个“...value = True”的问题。 - Kazimierz Jawor
我基本上什么都没有。我像这样找到了组框:Dim unit As Shape Set unit = Worksheets("All").Shapes("UnitType"),然后我需要从这个“unit”中找到所选的选项按钮。 - Firze
@KazJaw 说得好,更不用说使用了什么类型的选项按钮以及在哪里了吧?楼主能否详细说明一下。 - glh
1
@glh 我看到了这个,但是没有任何相关的内容。 - Firze
你至少可以在一个单元格中获取你的值,或者将其放在一个隐藏的工作表中。 - glh
显示剩余7条评论
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并重新运行之前,下面的代码不起作用。

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
分组形状和分组框不是同一物。 - chris neilsen
出现以下错误:运行时错误 '1004':此成员只能为组访问。 - Firze
我已经改进了我的答案,添加了额外的选项,可能解决您的问题。请记住答案中的注意事项 - Kazimierz Jawor

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

出现错误 运行时错误'91': 对象变量或 With 块变量未设置。在这一行上:“Debug.Print shpOpt.name” - Firze
似乎它从未进入这个if语句:“If shp.Value = 1 Then”。 - Firze
将那行代码加上断点,检查期望返回的选项按钮“Value”的值,并将“If”语句更改为测试该值。还要确保传递了正确的GroupBox名称(在我的测试中它可以正常工作)。 - chris neilsen
shp的意思是什么?我有点难以理解正在发生的事情。了解您变量的含义可能会有所帮助。 - Firze
尝试缩小问题范围。使用像/(#这样的特殊字符似乎会使VBA出现问题。 - Firze
显示剩余4条评论

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

Options

检查是否为“开启”:

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

2
问题是,这会测试工作表上的 所有 选项按钮,但 OP 希望仅测试特定分组框中的选项按钮。 - chris neilsen
这可能适用于单个选项按钮组。但是这不允许有多个选项按钮组。我大约会有10组每组10个选项按钮。 - Firze
@Chris 这可能有点痛苦,但你需要设置OLE对象 Set o = s.OLEFormat.Object 并使用 o.GroupBox.Name 来获取名称。 - glh
运行时错误 '91':对象变量或 With 块变量未设置。是的,我已将名称更改为 UnitType,这是我的组框名称。 - Firze
我完全是VB的新手,但这个语句:"Dim s As Shape, o"不是错的吗? - Firze
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接