PDA

View Full Version : [SOLVED:] Uncheck CheckBoxes if one is checked



YasserKhalil
07-26-2010, 01:02 AM
Hi everybody
How are you doing today?

I have five checkboxes on the worksheet ..
I'd like to untick the other four checkboxes if CheckBox1.Value=True
I wrote a code but I got an error
Please help me fix this code


Private Sub CheckBox1_Change()
If CheckBox1.Value = True Then
For X = 2 To 5
Me.Controls("CheckBox" & X).Value = False
Next X
End If
End Sub

Bob Phillips
07-26-2010, 01:15 AM
Private Sub CheckBox1_Change()
Dim x As Long
If Me.CheckBox1.Value Then
For x = 2 To 5
Me.OLEObjects("CheckBox" & x).Object.Value = False
Next x
End If
End Sub

YasserKhalil
07-26-2010, 01:31 AM
Perfect Mr. xld
Excellent dear

What if I want to do the same with other CheckBoxes .. In another words, when a checkbox is checked the others will be unchecked??
Is that code will be copied with other checkboxes change event?? or there's an easy way to do so??
Thanks a lot for your great help
You 're always helpful

Bob Phillips
07-26-2010, 01:44 AM
Private Sub CheckBox1_Change()
SetCheckBoxes 1
End Sub

Private Sub CheckBox2_Change()
SetCheckBoxes 2
End Sub

Private Sub CheckBox3_Change()
SetCheckBoxes 3
End Sub

Private Sub CheckBox4_Change()
SetCheckBoxes 4
End Sub

Private Sub CheckBox5_Change()
SetCheckBoxes 5
End Sub


Private Function SetCheckBoxes(CBIndex As Long)
Dim x As Long
If Me.OLEObjects("CheckBox" & CBIndex).Object.Value Then
For x = 1 To 5
If x <> CBIndex Then
Me.OLEObjects("CheckBox" & x).Object.Value = False
End If
Next x
End If
End Function

YasserKhalil
07-26-2010, 02:08 AM
Mr. xld
I can't believe myself
It works perfect..
Thank you very much from now till the end or even beyond the end of my life

drm51
04-29-2013, 08:28 AM
Hi xld,
The code you showed above and I listed below again, works great for checkboxes 1-5 alone, but I have many groups of checkboxes with 3 to 5 checkboxes in them that I would like to be independent of each other. If you check a checkbox in one group the other checkboxes in that group will be unchecked, but it would not affect the other groups of checkboxes.

I have tried the code you presented as below:


Private Sub CheckBox1_Change()
SetCheckBoxes 1
End Sub

Private Sub CheckBox2_Change()
SetCheckBoxes 2
End Sub

Private Sub CheckBox3_Change()
SetCheckBoxes 3
End Sub

Private Sub CheckBox4_Change()
SetCheckBoxes 4
End Sub

Private Sub CheckBox5_Change()
SetCheckBoxes 5
End Sub


Private Function SetCheckBoxes(CBIndex As Long)
Dim x As Long
If Me.OLEObjects("CheckBox" & CBIndex).Object.Value Then
For x = 1 To 5
If x <> CBIndex Then
Me.OLEObjects("CheckBox" & x).Object.Value = False
End If
Next x
End If
End Function

Like I stated, this code works great for just ONE group of checkboxes. How do I change it to work independently for multiple groups of checkboxes.

Thank you very much for any help you can provide.

drm51

drm51
04-29-2013, 08:52 AM
Hi,
I am referring back to a post “Solved: Uncheck Checkboxes if one is checked”, but have additional questions for this post. I am not familiar as how to reopen a “Solved” post, so I will post a new one.

The code showed above popst and I listed below again, works great for checkboxes 1-5 alone, but I have many groups of checkboxes with 3 to 5 checkboxes in them that I would like to be independent of each other. If you check a checkbox in one group the other checkboxes in that group will be unchecked, but it would not affect the other groups of checkboxes.

I have listed the code presented as below:


Private Sub CheckBox1_Change()
SetCheckBoxes 1
End Sub

Private Sub CheckBox2_Change()
SetCheckBoxes 2
End Sub

Private Sub CheckBox3_Change()
SetCheckBoxes 3
End Sub

Private Sub CheckBox4_Change()
SetCheckBoxes 4
End Sub

Private Sub CheckBox5_Change()
SetCheckBoxes 5
End Sub


Private Function SetCheckBoxes(CBIndex As Long)
Dim x As Long
If Me.OLEObjects("CheckBox" & CBIndex).Object.Value Then
For x = 1 To 5
If x <> CBIndex Then
Me.OLEObjects("CheckBox" & x).Object.Value = False
End If
Next x
End If
End Function

Like I stated, this code works great for just ONE group of checkboxes. How do I change it to work independently for each group of checkboxes for multiple groups of checkboxes.

Thank you very much for any help you can provide.

drm51

SamT
04-29-2013, 03:13 PM
DRM51,

Welcome to VBA Express.

This thread is solved, only people who want to see how it was solved will see your question.

Start a new thread, maybe titled, "Uncheck boxes in one group only.":dunno