Select one checkbox to Deselect another

G

geniussamu

I have five checkboxes from C7 to G7. I want the flow like this if I select
any one checkbox among those five the other four box should become unchecked
automatically. Please help.
 
G

geniussamu

I do not want to use the option button. I want to do the same with
checkboxes. Please tell me that it is possible or not.
 
K

Keith74

Yes, it's completely possible. You just have to add code to pick up
when the value of a checkbox changes and reset the other checkboxes.
Theres loads of code samples on the web.
 
S

Susan

if checkbox1.value = true then
checkbox2.value = false
checkbox3.value = false
end if

if checkbox2.value = true then
checkbox1.value = false
checkbox3.value = false
end if

if checkbox3.value = true then
checkbox1.value = false
checkbox2.value = false
end if

like that
hth
susan
 
S

Susan

using control toolbox checkboxes, i put them within _click events
(when they get checked):

Option Explicit

Private Sub CheckBox1_Click()

CheckBox2.Value = False
CheckBox3.Value = False

End Sub
==========================
Private Sub CheckBox2_Click()

CheckBox1.Value = False
CheckBox3.Value = False

End Sub
=========================
Private Sub CheckBox3_Click()

CheckBox1.Value = False
CheckBox2.Value = False

End Sub
=========================
did one for each checkbox, as mentioned previously.
hope it helps.
susan
 
C

Chip Pearson

Here's one way. In VBA, go to the "Insert" menu, choose "Module", and paste
the following code in that module.

''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Option Compare Text

Dim ObjCollection As Collection
Dim ChkCollection As Collection

Sub Auto_Open()
Dim WS As Excel.Worksheet
Dim CKBox As CCheck
Dim OleObj As Excel.OLEObject

Set ObjCollection = New Collection
Set ChkCollection = New Collection
Set WS = ThisWorkbook.Worksheets("Sheet1")
For Each OleObj In WS.OLEObjects
With OleObj
If TypeOf .Object Is MSForms.CheckBox Then
If Not Application.Intersect(WS.Range("C7:G7"), .TopLeftCell) Is
Nothing Then
Set CKBox = New CCheck
CKBox.AddCheckBox .Object
CKBox.SetCollection ChkCollection
ChkCollection.Add .Object
ObjCollection.Add CKBox
End If
End If
End With
Next OleObj
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''

Then, in VBA, go to the "Insert" menu and choose "Class Module". Press F4 to
bring up the "Properties" window and change the Name from "Class1" to
"CCheck". In that class module, paste the following code:

''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
Option Compare Text

Private WithEvents pChkBox As MSForms.CheckBox
Private pCheckBoxes As Collection
Private pIgnore As Boolean


Private Sub Class_Initialize()
Set pCheckBoxes = New Collection
End Sub

Friend Sub AddCheckBox(CHK As MSForms.CheckBox)
Set pChkBox = CHK
End Sub

Friend Sub SetCollection(C As Collection)
Set pCheckBoxes = C
End Sub

Private Sub pChkBox_Click()
Dim N As Long

If pChkBox.Value = 0 Then
Exit Sub ' unchecked. get out.
End If

If pIgnore = True Then
Exit Sub ' internal event. get out.
End If
On Error GoTo ErrH:
pIgnore = True
With pCheckBoxes
For N = 1 To .Count
If .Item(N).Caption <> pChkBox.Caption Then
.Item(N).Value = 0
End If
Next N
End With
ErrH:
pIgnore = False
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''


Finally, run the Auto_Open procedure to initialize things. (This will run
automatically when you open the workbook later.) The code makes all of the
check box objects whose Top Left Cell is in the range C7:G7 on Sheet1
mutually exclusive. Checking one will uncheck all the others. Check boxes
that are not within C7:G7 are not affected by the code. You can have as many
checkboxes as you want, name those checkboxes anything you want and the code
will continue to work. Note, though, that if you add checkboxes or change
their names, you'll need to run the Auto_Open procedure again.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 
C

Chip Pearson

My code assumed that all the check boxes have distinct captions. If this is
not the case, change the line of code in the Class Module from

If .Item(N).Caption <> pChkBox.Caption Then
to
If Not .Item(N) Is pChkBox Then


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top