Userform Checkboxes

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
834
So my following code successfully only allows either checkbox 1 or checkbox 2 to be ticked at one time.

I want to now expand this with Checkbox 8 OR checkbox 9.

I need to know how to adapt the code below to achieve this please?

Thanks.

VBA Code:
Private Sub CheckBox1_Change()

Dim ctl As MSForms.Control

If Me.CheckBox1.Value = True Then
 
 For Each ctl In Me.Controls
 If TypeName(ctl) = "CheckBox" And ctl.Name <> "CheckBox1" Then
 ctl.Value = False
 End If
 Next ctl
 End If

End Sub
 
Private Sub CheckBox2_Change()

Dim ctl As MSForms.Control

If Me.CheckBox2.Value = True Then
 
 For Each ctl In Me.Controls
 If TypeName(ctl) = "CheckBox" And ctl.Name <> "CheckBox2" Then
 ctl.Value = False
 End If
 Next ctl
 End If

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Is there a reason you have to use check boxes? If it's an either/or option you should probably use groups of option buttons instead.
 
Upvote 0
You can definitely adapt the code but honestly option buttons are your better bet. No code necessary. Option buttons are designed specifically for exactly this situation - when you want one option OR another. You can group option buttons together using, say, a frame control for example then at run time you will only be able to select one option within each group at a time. You can have as many option buttons in each group as you like - it isn't restricted to just two.

Still, if you really want to use check boxes instead I can help you adapt your code but I'll have to post back later as I'm in work at the moment and am about to go to a meeting. Hopefully someone else will post a response in the meantime so you're not waiting around but if not I'll be back later...
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

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