If CheckBox1 is selected cannot select CheckBox2. If CheckBox2 is selected cannot select CheckBox1

Kyle M

Board Regular
Joined
Apr 13, 2010
Messages
65
I created a UserForm with 2 CheckBoxes. CheckBox1 is labeled Yes while CheckBox2 is labeled No. How can I format the UserForm so if CheckBox1 is selected CheckBox2 cannot be selected and if CheckBox2 is selected CheckBox1 cannot be selected? Below is the code I am using but it is not doing anything. I am not even getting an error message. Can you help?

Code:
[FONT=Times New Roman][SIZE=3]Sub CheckBoxes()[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Dim bChk1 As Boolean, bChk2 As Boolean[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p>[FONT=Times New Roman][SIZE=3] [/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3]Sub CheckBox1_Click()[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Application.EnableEvents = False[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]If bChk2 = False Then Exit Sub[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]bChk1 = Not bChk2[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Call CheckBox2_Click[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Application.EnableEvents = True[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]
<o:p>[FONT=Times New Roman][SIZE=3] [/SIZE][/FONT]</o:p>
[FONT=Times New Roman][SIZE=3]Sub CheckBox2_Click()[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Application.EnableEvents = False[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]If bChk1 = False Then Exit Sub[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]bChk2 = Not bChk1[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Call CheckBox1_Click[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]Application.EnableEvents = True[/SIZE][/FONT]
[FONT=Times New Roman][SIZE=3]End Sub[/SIZE][/FONT]
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Wouldn't it be easier to use 2 option buttons?

If you do want 2 checkboxes that code isn't going to work because you don't refer to the checkboxes anywhere.

Unless that is they are called bChk1 & bChk2, then the event code wouldn't get triggered.

Here's code for 2 checkboxes called Checkbox1 and Checkbox2.

It'll actually prevent the user clicking one checkbox when the other is checked.
Code:
Private Sub CheckBox1_Click()
    CheckBox2.Locked = CheckBox1.Value
End Sub

Private Sub CheckBox2_Click()
    CheckBox1.Locked = CheckBox2.Value
End Sub
I'm not sure that's what you want though.:)

This will 'alternate' between them being checked.
Code:
Private Sub CheckBox1_Click()
    CheckBox2.Value = Not CheckBox1.Value
End Sub

Private Sub CheckBox2_Click()
    CheckBox1.Value = Not CheckBox2.Value
End Sub
 
Upvote 0
SOLVED: If CheckBox1 is selected cannot select CheckBox2. If CheckBox2 is selected cannot select CheckBox1

Norie, this is super! thank you for your help. It is very helpful.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,762
Messages
6,126,736
Members
449,334
Latest member
moses007

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