Search code examples
excelvbaradio-buttongroupbox

VBA Excel RadioButton does not work properly until i delete another RadioButton and undo


I am currently working on Option-buttons in VBA. I want to add a Group-box, in which there are 6 Option-buttons (all connected to a fixed cell somewhere). Only at max one of those Option-buttons shall be able to be active.

It should look like this:

It should look like this.

I want to create this with a VBA macro. The problem is, that when I create this, Button-1 and Button-6 are connected. If I select one, I select both. All other Radio-buttons are not like this, and can be clicked separately.

Additionally, if I manually delete one of those Radio-buttons, and click Ctrl+Y, everything works just fine.. then all 6 buttons can be selected separately.

Do you have any idea what I could do, such I do not have to first delete one button and undo this to make this work properly?

Edit: Code added

Sub ButtonsInABox()
    ActiveSheet.GroupBoxes.Delete
    ActiveSheet.OptionButtons.Delete
startcell = Array(1, 1)

        Add_GroupBox Array(startcell(0), startcell(1))
            Add_RadioButton Array(startcell(0), startcell(1)), "Button-1", "A11"
            Add_RadioButton Array(startcell(0), startcell(1) + 1), "Button-2", "A11"
            Add_RadioButton Array(startcell(0), startcell(1) + 2), "Button-3", "A11"
            Add_RadioButton Array(startcell(0) + 1, startcell(1)), "Button-4", "A11"
            Add_RadioButton Array(startcell(0) + 1, startcell(1) + 1), "Button-5", "A11"
            Add_RadioButton Array(startcell(0) + 1, startcell(1) + 2), "Button-6", "A11"

End Sub


Sub Add_RadioButton(startcell, ButtonName, corresponding_cell)
   a = startcell(0)
   b = startcell(1)
   xx = Cells(a, b).Address(RowAbsolute:=False, ColumnAbsolute:=False)

   ActiveSheet.OptionButtons.Add(Range(xx).Left, Range(xx).Top, Range(xx).Width * 1, 4).Select
   With Selection
    .ShapeRange.ScaleHeight 0.65, msoFalse
    .Characters.Text = ButtonName
    .LinkedCell = corresponding_cell
    .Display3DShading = True
    End With

End Sub


Sub Add_GroupBox(startcell)
    a = startcell(0)
    b = startcell(1)
    xx = Cells(a, b).Address(RowAbsolute:=False, ColumnAbsolute:=False)


    ActiveSheet.GroupBoxes.Add(Range(xx).Left, Range(xx).Top, Range(xx).Width * 3, Range(xx).Height * 2).Select

    Selection.Characters.Text = ""
End Sub

Solution

  • You are experiencing the issue because of the line

    ActiveSheet.GroupBoxes.Add(Range(xx).Left, Range(xx).Top, _
    Range(xx).Width * 3, Range(xx).Height * 2).Select
    

    If you change it to

    ActiveSheet.GroupBoxes.Add(Range(xx).Left, Range(xx).Top, _
    Range(xx).Width * 3.5, Range(xx).Height * 2.5).Select
    

    It will work. Seems like Excel is getting confused whether the object is inside the group or not. Better to include extra margins around the option buttons.

    By the way, avoid using .Select. Work with object directly as shown in the example below.


    Alternative

    If you want there is one more way to achieve what you want. This method doesn't use a GroupBox. It groups the controls together. See this example

    Sub Sample()
        Dim shp As Variant
        Dim ShpGroup As Variant
        Dim ws As Worksheet
        Dim startcell As Variant
        Dim i As Long, j As Long, k As Long
        Dim CellAddr As String
        Dim Shapenames As String
        Dim ShpAr(1 To 6) As Variant
    
        Set ws = Sheet1
    
        For Each shp In ws.Shapes
            shp.Delete
        Next shp
    
        k = 1
    
        For i = 1 To 2
            For j = 1 To 3
                CellAddr = Cells(i, j).Address(RowAbsolute:=False, ColumnAbsolute:=False)
                Set shp = ws.OptionButtons.Add(Range(CellAddr).Left, Range(CellAddr).Top, Range(CellAddr).Width, 4)
    
                With shp
                    .Name = "Button " & k
                    .ShapeRange.ScaleHeight 0.65, msoFalse
                    .Characters.Text = "Button " & k
                    .LinkedCell = "A11"
                    .Display3DShading = True
                End With
    
                '~~> Store the shape name in an array
                ShpAr(k) = "Button " & k
                k = k + 1
            Next j
        Next i
    
        Set ShpGroup = ws.Shapes.Range(ShpAr).Group
    End Sub