OptionButtons collection not recognized

Corni

Active Member
Joined
Mar 2, 2002
Messages
328
I have a user form in Excel XP with two frames and two groups of option buttons, 17 in the first frame and two in the second frame. To better handle the two groups I assigned the GroupName property for the two groups as well. When I am trying to run this code:

For Each opt In UserForm1.OptionButtons
'do something
Next opt

I get following message 'Method or data member not found' (OptionButtons is highlighted). Any idea what goes wrong?
 

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

TommyGun

MrExcel MVP
Joined
Dec 9, 2002
Messages
4,202
That is because there is not an OptionButtons collection for a UserForm.

You must access the Controls collection to do what you are looking for.

<font face=Courier New>    <SPAN style="color:#00007F">Dim</SPAN> ctr <SPAN style="color:#00007F">As</SPAN> Control
    
    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ctr <SPAN style="color:#00007F">In</SPAN> Me.Controls
        <SPAN style="color:#00007F">If</SPAN> TypeName(ctr) = "OptionButton" <SPAN style="color:#00007F">Then</SPAN>
            <SPAN style="color:#007F00">'do some code here</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN></FONT>
 

Marc

Active Member
Joined
Feb 21, 2002
Messages
388
Perhaps you could modify your code as shown within:

Private Sub CommandButton1_Click()
XX = UserForm1.Frame1.Controls.Count
For Each Opt In UserForm1.Frame1.Controls

MsgBox XX
XX = XX - 1

Next Opt

End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,492
Messages
5,601,994
Members
414,490
Latest member
Rip181

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
Top