Similarly from your code, Button1_Click() is an event as well. What it's used for is specifying the userform to do something when userform is initialized (first started). It is an Excel-VBA functionality called Event. No, you don't need to have UserForm_Initialize() method. Set opt2 = ("Forms.OptionButton.1",, True)įrom seeing your edited post and your comment. Set opt1 = ("Forms.OptionButton.1",, True) 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. Your GroupName is a string that allows Excel to recognize the option buttons are linked together. Your Caption is a string that appear on your userform to the users. Your control Name is only there for you to refer it back for changing/reading.
Remember in using option buttons, your option buttons need to share the same GroupName.
#Adding a new line in excel vba on click 2016 userform code#
In your "button_onclick()" procedure stored in a module add this code: 'This is set by the code in UserForm1Īnd when you click submit a message box will pop up showing you which option was selected To pull from a range like this Dim OptionList() as Variant If you want to pull your list from a sheet you can change Dim OptionList(1 To 3) As String SelectedOption = UserForm1.Controls(i).Caption UserForm1.Height = UserForm1.Height + btn.Height + (0.5 * opt.Height)įor i = 0 To - 1 Set opt = ("Forms.OptionButton.1", "radioBtn" & i, True)ītn.Top = UserForm1.Height - btn.Height + (0.5 * opt.Height)ītn.Left = (UserForm1.Width * 0.5) - (btn.Width * 0.5) You can set the Initialize method for your UserForm to programmatically create a group of radio buttons Private Sub UserForm_Initialize() If you have a form named UserForm1 that contains a button named CommandButton1 There is button on excel sheet Button1 I am calling this function on clicking that button. I did not use initialize function on userform. I tried to add a single button on the userform. UserForm1 is the userform which I created using Insert option in VBA menu bar. Set rad = ("Forms.OptionButton.1", "radioFoo", True) ' Some operatin is done here to get the list of values in lResult LResult As Variant ' this is a array which contains string vaues to be dispayed as radio button. So anybody please give information regarding this. 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.
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. 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.