Skip to main content

How to uncheck another checkbox when a checkbox is checked in Excel?

Author: Siluvia Last Modified: 2023-01-11

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.

Uncheck another checkbox when a new checkbox is checked with VBA code


Uncheck another checkbox when a new checkbox is checked with VBA code

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

Notes:

1) In the code, Checkbox1, Checkbox2, ..., Checkbox10 are the name of the checkboxes;
2) 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")
3) The checkboxes are ActiveX Control checkboxes.

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.


Demo: Uncheck another checkbox when a checkbox is checked in Excel

  Kutools for Excel includes 300+ powerful features for Microsoft Excel. Free to try with no limitation in 30 days. Download now!

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

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...

Description


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!
Comments (0)
No ratings yet. Be the first to rate!
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations