Home » excel » How can I find the shape that is selected in group box in Excel?

How can I find the shape that is selected in group box in Excel?

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a group box with option buttons in it and I need to find out which one of them is selected in VBA. I have been browsing MSDN for hours now and I can’t find a solution.

There has to be a way to find the selected option button. Possibly find the group by name and for-each through each option button?

How to&Answers:

Here’s what seems to be a working solution.

(Nod to KazJaw for Dim ... As OptionButton. this seems to be the key to get .GroupBox to work)

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

Use it like this

Sub test()
    Dim shpOpt As OptionButton

    Set shpOpt = WhichOption(Worksheets("Sheet1").Shapes("Group Box 1"))
    Debug.Print shpOpt.Name
End Sub

Answer:

If you really need to check OptionButton which are grouped (Grouped in the way we group any type of shape) you could go with this code:

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

Edit having in mind the following picture the code above will solve the problem if we have Option Buttons which are group in the way we group any Shapes placed in the sheet.

The code under the picture will find which option button is selected if they are located within GroupBox. Code check the name of the group in which OptionButton is located.

Important Note! the code below didn’t work until I switched Excel off and run it again.

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

Answer:

Lets say you have two standard option buttons:

Options

To check if its “on” use:

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

To get its alternat text use:

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

For a large amount of options the “FormControlType” property can be used:

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

If you wanted a particular group:

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