简体   繁体   中英

Adding OptionButtons to the Userform programatically in VBA Excel

I am very new to VBA programming. My scenario is I will get a list of String values I need these values to be displayed to the user using radio buttons on a small window so that whenever the user selects any value by clicking on the radio button I should be able to get that value in the VBA code. I searched for adding options button in the user form in the internet I got some solution which use GUI method of creating option buttons. But I need it done through program. I found a helpful thread in stackoverflow ( How can I dynamically add a radio button on a form using VBA ) I used this but still I am unable to get any label or button on the user form, a plain userform will be displayed. So anybody please give information regarding this.

The code is :

Sub Button1_Click()
    lResult As Variant    ' this is a array which contains string vaues to be dispayed as radio button.

    ' Some operatin is done here to get the list of values in lResult

    Dim rad As Variant
    Set rad = UserForm1.Controls.Add("Forms.OptionButton.1", "radioFoo", True)
    rad.Caption = "bar"
    rad.Left = 10
    rad.Width = 10
    rad.Top = 10
End Sub

UserForm1 is the userform which I created using Insert option in VBA menu bar. I tried to add a single button on the userform. I did not use initialize function on userform. There is button on excel sheet Button1 I am calling this function on clicking that button.

Thank you

If you have a form named UserForm1 that contains a button named CommandButton1

用户窗体



You can set the Initialize method for your UserForm to programmatically create a group of radio buttons

Private Sub UserForm_Initialize()
    Dim OptionList(1 To 3) As String
    Dim btn As CommandButton
    Set btn = UserForm1.CommandButton1
    Dim opt As Control
    Dim s As Variant
    Dim i As Integer

    OptionList(1) = "Option 1"
    OptionList(2) = "Option 2"
    OptionList(3) = "Option 3"

    For Each s In OptionList
        Set opt = UserForm1.Controls.Add("Forms.OptionButton.1", "radioBtn" & i, True)
        opt.Caption = s
        opt.Top = opt.Height * i
        opt.GroupName = "Options"

        UserForm1.Width = opt.Width
        UserForm1.Height = opt.Height * (i + 2)

        i = i + 1
    Next

    btn.Caption = "Submit"
    btn.Top = UserForm1.Height - btn.Height + (0.5 * opt.Height)
    btn.Left = (UserForm1.Width * 0.5) - (btn.Width * 0.5)

    UserForm1.Height = UserForm1.Height + btn.Height + (0.5 * opt.Height)
End Sub

Private Sub CommandButton1_Click()
    Dim i As Integer

    For i = 0 To UserForm1.Controls.Count - 1
        If UserForm1.Controls(i) Then
            SelectedOption = UserForm1.Controls(i).Caption
        End If
    Next

    UserForm1.Hide
End Sub



If you want to pull your list from a sheet you can change

Dim OptionList(1 To 3) As String

OptionList(1) = "Option 1"
OptionList(2) = "Option 2"
OptionList(3) = "Option 3"

to pull from a range like this

Dim OptionList() as Variant
OptionList = Range("A1:A3")


In your "button_onclick()" procedure stored in a module add this code:

'This is set by the code in UserForm1
Public SelectedOption As String

Sub Button1_OnClick()
    UserForm1.Show
    MsgBox SelectedOption
End Sub



Which gets you this result:

在此输入图像描述

And when you click submit a message box will pop up showing you which option was selected

在此输入图像描述

Remember in using option buttons, your option buttons need to share the same GroupName.
Your control Name is only there for you to refer it back for changing/reading.
Your Caption is a string that appear on your userform to the users.
Your GroupName is a string that allows Excel to recognize the option buttons are linked together.

So, if opt1's GroupName is "1" while opt2's GroupName is "2", then you will be able to select both since they are in different Groups.

Private Sub UserForm_Initialize()
    Dim opt1 As Control, opt2 As Control

    Set opt1 = UserForm1.Controls.Add("Forms.OptionButton.1", , True)
    With opt1
        .Name = "radioFoo"
        .GroupName = "1"
        .Caption = "Option 1"
    End With

    Set opt2 = UserForm1.Controls.Add("Forms.OptionButton.1", , True)
    With opt2
        .Name = "radioFoo2"
        .GroupName = "1"
        .Caption = "Option 2"
        .Left = 100
    End With

End Sub


EDIT:
From seeing your edited post and your comment...
No, you don't need to have UserForm_Initialize() method.

It is an Excel-VBA functionality called Event .
What it's used for is specifying the userform to do something when userform is initialized (first started).
Similarly from your code, Button1_Click() is an event as well.
Since you are telling Excel to do the following at the event where Button1 is clicked by the user...

Anyways, let me briefly explain to you what option buttons do.
A group of option buttons forces the user to select only one option out of options given by the program.
And an option button in VBA only allows you to create one option. So, if you want to create 2 options, you must create 2 option buttons.
But there is just one problem: what if you want to create 2 groups of option buttons so that the user can select 2 separate options? For example, food and drinks?
VBA presents us a property of an option button called GroupName . GroupName allows VBA to distinguish between separate groups of option buttons.
Therefore, in every option button you create, it is essential that you initialize its GroupName value. If you see any implementation of option button without GroupName, you are playing with fire.

So, let's finally take a look at your code:

Sub Button1_Click()
    ' Some operatin is done here to get the list of values in lResult

    Dim rad1 As Control, rad2 As Control
    Set rad1 = UserForm1.Controls.Add("Forms.OptionButton.1", "radioFoo1", True)
    rad1.Caption = "bar"
    rad1.Left = 10
    rad1.Width = 10
    rad1.Top = 10
    rad1.GroupName = "Group1"

    Set rad2 = UserForm1.Controls.Add("Forms.OptionButton.1", "radioFoo2", True)
    rad2.Caption = "foo"
    rad2.Left = 10
    rad2.Width = 10
    rad2.Top = 50
    rad2.GroupName = "Group1"
End Sub

Just one thing:
- As I've implicitly mentioned before, an option button with only one option does not mean anything. If you are looking for on/off kind of functionality, you might as well go with checkbox.
So, I've created another option button defining it to be in the same group as the first option button you've created (rad1).

Hope it helps.

Cheers,
kpark

Be sure to select the best answer when the question/problem has been answered/solved. Thanks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM