Userforms and options buttons

Jonnyoforem

New Member
Joined
May 22, 2015
Messages
32
I have a macro that allows me to loop through option buttons on my worksheet and then assign the name of the option button to the variable buttonName as shown in the code below.

Code:
Dim myButton As OptionButton
Dim buttonName As String

'Loop Through Option Buttons
For Each myButton In Worksheets("Search").OptionButtons
If myButton.Value = 1 Then
buttonName = myButton.Text
Exit For
End If
Next myButton
Code:

I want to do the same thing but instead of looping through option buttons on a worksheet, I have a user form with option buttons I want to loop through. It's very important that I'm able to assign a variable to whatever the name of the selected option button is because I'll be using that variable later in the code. What tweaks do i need to make to the above code to accomplish this? Thanks in advance for any help
 
Rick

I had understood perfectly what you meant. Agree your method is faster and safer (no misspelling).

M.
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Just a word of caution on this (and one reason I generally use TypeName in this situation): if you are interested only in checkboxes on a form, you will find that this:
Code:
TypeOf ctl Is MSForms.CheckBox
returns True for Checkboxes, but also for OptionButtons and ToggleButtons.

TypeName returns the actual type of a control (or variable) whereas TypeOf detects whether an object implements a specific interface.
 
Upvote 0
Just a word of caution on this (and one reason I generally use TypeName in this situation): if you are interested only in checkboxes on a form, you will find that this:
Code:
TypeOf ctl Is MSForms.CheckBox
returns True for Checkboxes, but also for OptionButtons and ToggleButtons.

TypeName returns the actual type of a control (or variable) whereas TypeOf detects whether an object implements a specific interface.

Good to know!

M
 
Upvote 0
Just a word of caution on this (and one reason I generally use TypeName in this situation): if you are interested only in checkboxes on a form, you will find that this:
Code:
TypeOf ctl Is MSForms.CheckBox
returns True for Checkboxes, but also for OptionButtons and ToggleButtons.

TypeName returns the actual type of a control (or variable) whereas TypeOf detects whether an object implements a specific interface.
OMG :eek: Are you kidding me??!!!?!!?!

Obviously I did not know this, so thank you very much for bring it to my (our) attention. Really though, why on earth would Microsoft have done that? I mean, there is an MSForms.OptionButton and an MSForms.ToggleButton type member which can be checked against, so why did Microsoft think it necessary to widen the scope of MSForms.Checkbox to cover them? I just don't get it.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,636
Messages
6,125,951
Members
449,276
Latest member
surendra75

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