How do I enable/disable checkboxes in relation to one another?

Ayugma

New Member
Joined
Nov 20, 2020
Messages
39
Office Version
  1. 2016
Platform
  1. Windows
I have two check boxes: CheckBox2 and CheckBox5. If CheckBox2 is clicked, then CheckBox5 should be disabled. When CheckBox5 is clicked CheckBox2 is disabled. I attempted this by using the following code:

VBA Code:
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
    CheckBox5.Enabled = False
    CheckBox5.Value = False
End If
End Sub

Private Sub CheckBox5_Click()
If CheckBox5.Value = True Then
    CheckBox2.Enabled = False
    CheckBox2.Value = False
End If
End Sub

The code however does not work as intended. For some reason, CheckBox5 remains disabled, even after I have unchecked CheckBox2. Any help would be appreciates. NOTE: No I can't switch to OptionButtons.
 

Some videos you may like

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,481
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
You are only checking if the value is true, you need to add code to re-enable the checkbox if the value is false.
 
Solution

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,482
Office Version
  1. 2019
Platform
  1. Windows
Hi,
welcome to forum
If you check say checkbox2 it disables the other - you will therefore, first have uncheck it before being able to check checkbox5

Perhaps if I have understood correctly, something like this will do what you want??

VBA Code:
Private Sub CheckBox2_Click()
    Me.CheckBox5.Enabled = Not Me.CheckBox2.Value
End Sub

Private Sub CheckBox5_Click()
    Me.CheckBox2.Enabled = Not Me.CheckBox5.Value
End Sub

Dave
 

Ayugma

New Member
Joined
Nov 20, 2020
Messages
39
Office Version
  1. 2016
Platform
  1. Windows
Fluff's response worked. So did dmt32. I do wonder what "Me" and "Not Me" refer to? I don't have a significant VBA experience, so if you could explain what they are and how they should be used?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,481
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

It depends on where the code is located.
In a Userform Me is the userform, if it's in a sheet module Me is the sheet.
If checkbox2 is true then the enabled property of checkbox5 is set to Not true, ie false & vice versa
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,481
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,472
Messages
5,624,925
Members
416,064
Latest member
PaulBr2

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