Dynamically add optionboxes on userform

Robb

Board Regular
Joined
Feb 17, 2002
Messages
145
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
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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
 
Upvote 0

Forum statistics

Threads
1,213,558
Messages
6,114,297
Members
448,564
Latest member
ED38

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top