OptionButtons being treated as CheckBoxes in If statement??

sramsay

Board Regular
Joined
Feb 19, 2015
Messages
96
Basically, I have a little loop, that that sets all CheckBoxes to true or False based on the value of an OptionButton.

Works great except that it also meets the IF condition for the option buttons, even though I specifically wrote that I want CheckBox Values to change.

Am I missing anything obvious?



Code:
Dim ctrl As MSForms.Control

If Me.OptionButton1.value = True Then
    For Each ctrl In UserForm6.Controls
        If TypeOf ctrl Is MSForms.CheckBox Then
            ctrl.value = False
        End If
    Next ctrl
    Exit Sub
ElseIf Me.OptionButton2.value = True Then
    For Each ctrl In UserForm6.Controls
        If TypeOf ctrl Is MSForms.CheckBox Then
            ctrl.value = True
        End If
    Next ctrl
End If
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
How about
Code:
If Me.OptionButton1.Value = True Then
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "CheckBox" Then ctrl.Value = False
    Next ctrl
    Exit Sub
ElseIf Me.OptionButton2.Value = True Then
 
Upvote 0
Alternatively, how about
Code:
   Dim ctrl As MSForms.Control
   For Each ctrl In Me.Controls
      If TypeName(ctrl) = "CheckBox" Then ctrl.Value = Me.OptionButton2.Value
   Next ctrl
 
Upvote 0
All methods work a treat. Thanks guys.
Although it's going to do my head in why my original approach didn't work.

I added debug.prints and it was only OptionButtons that were being included with the CheckBoxes, other objects were fine :confused:
 
Upvote 0
Glad we could help.

Like MickG, I don't understand why it fails.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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