Consulting

Results 1 to 8 of 8

Thread: Uncheck CheckBoxes if one is checked

  1. #1

    Uncheck CheckBoxes if one is checked

    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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

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

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

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

  6. #6
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    3
    Location
    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

  7. #7
    VBAX Newbie
    Joined
    Apr 2013
    Posts
    3
    Location
    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

  8. #8
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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."
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •