Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 2 of 2

Thread: Dynamically add optionboxes on userform

  1. #1
    Board Regular
    Join Date
    Feb 2002
    Location
    Winnipeg, Manitoba, CANADA
    Posts
    144
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Good day all, a brain teser possibly...
    I want to dynamically add optionboxes to a Frame on a userform. The optionboxes are tied to a defined range. There will be 3 sets of frames w/optionboxes on the form. So if the Range is modified the correct amount of optionboxes would appear. Below is my function that doesn't work but it's a start....

    Function AddOptionButton(OpArray, Default, FrameTitle)
    Dim NewOptionButton As MSForms.optionbutton
    Dim Frame As MSForms.Frame
    Dim i As Integer, TopPos As Integer
    Dim MaxWidth As Long

    TopPos = 10
    Set Frame = SetupFile.Controls.Add("forms.frame.1")
    With Frame
    .Caption = FrameTitle
    .Enabled = True
    .Controls.Add ("forms.OptionButton.1") '?? = .Caption = OpArray(i)
    End With

    ' This works but how do I add it to the frame?
    ' Add the OptionButtons
    TopPos = 4
    MaxWidth = 0 'Stores width of widest OptionButton
    For i = LBound(OpArray) To UBound(OpArray)
    Set NewOptionButton = SetupFile.Controls.Add("forms.OptionButton.1")
    With NewOptionButton
    .Width = 800
    .Caption = OpArray(i)
    .Height = 15
    .Left = 8
    .Top = TopPos
    .Tag = i
    .AutoSize = True
    If Default = i Then .Value = True
    If .Width > MaxWidth Then MaxWidth = .Width
    End With
    TopPos = TopPos + 15
    Next i
    End Function
    Thanx.

  2. #2
    MrExcel MVP Al Chara's Avatar
    Join Date
    Feb 2002
    Location
    Newark, Delaware
    Posts
    1,701
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Try this code below. I got rid of some things. To add the option button to the frame, you use this command, "Set NewOptionButton = SetupFile.Frame.Controls.Add("forms.OptionButton.1")" This should work for you.

    Kind regards,
    Al

    Function AddOptionButton(OpArray, Default, FrameTitle)
    Dim NewOptionButton, frame As control
    Dim i, TopPos As Integer
    Dim MaxWidth As Long
    TopPos = 10
    Set Frame = SetupFile.Controls.Add("forms.frame.1")
    With Frame
    .Caption = FrameTitle
    .Enabled = True
    End With

    TopPos = 4
    MaxWidth = 0
    For i = LBound(OpArray) To UBound(OpArray)
    Set NewOptionButton = SetupFile.Frame.Controls.Add("forms.OptionButton.1")
    With NewOptionButton
    .Width = 800
    .Caption = OpArray(i)
    .Height = 15
    .Left = 8
    .Top = TopPos
    .Tag = i
    .AutoSize = True
    If Default = i Then .Value = True
    If .Width > MaxWidth Then MaxWidth = .Width
    End With
    TopPos = TopPos + 15
    Next i
    End Function

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •