How to uncheck another checkbox when a checkbox is checked in Excel?
As shown in the gif below, suppose there is a purchase list that needs to be finalized by you. While checking the list, you find that the checked items in some categories are over budget and need to reselect. Since the list is too long, you now need a more efficient way to automatically uncheck the original ticked checkbox when a new checkbox is checked in a category. This tutorial demonstrates the method step-by-step to help you get it done.
As shown in the above demo, ten checkboxes named Checkbox1, Checkbox2, Checkbox3, ..., Checkbox10 are divided into 3 groups and located in different categories in the table.
In this example, Checkbox1, 2, 3 are in one group, Checkbox4, 5, 6, 7 are in one group, and Checkbox8, 9,10 are in the same group. In each group, only one checkbox is allowed to be checked at a time. When a checkbox is checked, another checkbox is unchecked automatically.
Now let’s see how to apply the following VBA code to solve this problem.
1. Right click the sheet tab and click View Code from the context menu.
2. In the opened Microsoft Visual Basic for Applications window , paste the following VBA code into the Sheet (Code) window.
VBA code: Uncheck another checkbox when a new checkbox is checked
Dim xBol As Boolean 'Updated by Extendoffice 20220816 Private Sub CheckBox1_Change() SetCheckBoxes "CheckBox1" End Sub Private Sub CheckBox2_Change() SetCheckBoxes "CheckBox2" End Sub Private Sub CheckBox3_Change() SetCheckBoxes "CheckBox3" End Sub Private Sub CheckBox4_Change() SetCheckBoxes "CheckBox4" End Sub Private Sub CheckBox5_Change() SetCheckBoxes "CheckBox5" End Sub Private Sub CheckBox6_Click() SetCheckBoxes "CheckBox6" End Sub Private Sub CheckBox7_Click() SetCheckBoxes "CheckBox7" End Sub Private Sub CheckBox8_Click() SetCheckBoxes "CheckBox8" End Sub Private Sub CheckBox9_Click() SetCheckBoxes "CheckBox9" End Sub Private Sub CheckBox10_Click() SetCheckBoxes "CheckBox10" End Sub Private Function SetCheckBoxes(mCheckBoxName As String) Dim x As Long Dim xAllArr Dim xArrItem Dim xI, xJ If Not xBol Then Exit Function 'In the following line, the checkboxes enclosed in double quotes belong to the same group, and each checkbox is separated by a comma. To add more checkbox groups, please enclose the checkboxes in new double quotes. xAllArr = Array("CheckBox1,CheckBox2,CheckBox3", "CheckBox4,CheckBox5,CheckBox6,CheckBox7", "CheckBox8,CheckBox9,CheckBox10") For xI = LBound(xAllArr) To UBound(xAllArr) If InStr(xAllArr(xI), mCheckBoxName) > 0 Then xBol = False xArrItem = Split(xAllArr(xI), ",") For xJ = LBound(xArrItem) To UBound(xArrItem) If xArrItem(xJ) <> mCheckBoxName Then Me.OLEObjects(xArrItem(xJ)).Object.Value = False End If Next End If Next xBol = True End Function Private Sub Worksheet_Activate() xBol = True End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range) xBol = True End Sub
3. Press the Alt + Q keys to close the Microsoft Visual Basic for Applications window.
Then when you check a new checkbox in a group, the original checked one will be unchecked automatically as shown in the gif below.
Best Office Productivity Tools
Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time. Click Here to Get The Feature You Need The Most...
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!