Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Remember in using option buttons, your option buttons need to share the same GroupName.<br/> Your control <strong>Name</strong> is only there for you to refer it back for changing/reading.<br/> Your <strong>Caption</strong> is a string that appear on your userform to the users. <br/> Your <strong>GroupName</strong> is a string that allows Excel to recognize the option buttons are linked together.<br/></p> <p>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.</p> <pre><code>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 </code></pre> <p><br/> <strong><em>EDIT:</em></strong><br/> From seeing your edited post and your comment...<br/> No, you don't need to have UserForm_Initialize() method.<br/> <br/> It is an Excel-VBA functionality called <strong>Event</strong>.<br/> What it's used for is specifying the userform to do something when userform is initialized (first started). <br/> Similarly from your code, Button1_Click() is an event as well.<br/> Since you are telling Excel to do the following at the event where Button1 is clicked by the user...<br/> <br/> Anyways, let me briefly explain to you what option buttons do.<br/> A <strong>group</strong> of option buttons forces the user to select only one option out of options given by the program.<br/> 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.<br/> 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?<br/> VBA presents us a property of an option button called <strong>GroupName</strong>. GroupName allows VBA to distinguish between separate groups of option buttons.<br/> 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.<br/><br/></p> <p>So, let's finally take a look at your code:</p> <pre><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 </code></pre> <p>Just one thing:<br/> - 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.<br/> So, I've created another option button defining it to be in the same group as the first option button you've created (rad1).</p> <p>Hope it helps.<br/></p> <p>Cheers,<br/> kpark<br/></p> <p>Be sure to select the best answer when the question/problem has been answered/solved. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload