set OptionButton GroupName with excel VBA

Citizens Advice Mid Bedfordshire 21 Reputation points
2022-02-01T18:00:07.943+00:00

Hi
I can create ActiveX option buttons on a Excel worksheet and can change the Linked Cell but not the GroupName. Here is my code.
Any help would be much appreciated. Thanks, Jonathan

Sub AddMainButtons()
Dim Str As String
Application.ScreenUpdating = False
Sheet = ActiveSheet.Name
i = InputBox("How many rows do you want (max 40)", "Add Buttons", "Type number here")
For row = 1 To i

    ActiveSheet.OLEObjects.Add(ClassType:="Forms.OptionButton.1", Link:=False, _
    DisplayAsIcon:=False, Left:=83, Top:=60 * row - 12, Width:=10, Height:=10).Select
    Selection.LinkedCell = "AE" & row + 10

    Selection.GroupName = "group11"   'THIS IS THE LINE THAT DOESN'T WORK

Next
Application.ScreenUpdating = True
End Sub

Developer technologies Visual Basic for Applications
0 comments No comments
{count} votes

Accepted answer
  1. kinuasa 371 Reputation points
    2022-02-02T06:42:08.587+00:00

    Hi,
    You need to get the OptionButton object from the Object property of OLEObject.

    Public Sub Sample()
      With ActiveSheet.OLEObjects.Add(ClassType:="Forms.OptionButton.1", Link:=False, DisplayAsIcon:=False, Left:=10, Top:=10, Width:=10, Height:=10) 'OLEObject
        With .Object 'OptionButton
          .GroupName = "group11"
        End With
      End With
    End Sub
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.