Note: The other languages of the website are Google-translated. Back to English
English English

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.

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.


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

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