Maximum 2 checkboxes to be checekd in a group

Holy Inquisitor

New Member
Joined
Sep 17, 2013
Messages
6
I managed to come up with the following code for a checkbox group including 5 checkboxes.

Although it limits the possible selections to maximum 2, when 2 checkboxes are already selected it not always disables all other unselected checkboxes and in this case one of the previously selected checkboxes will become unchecked.

Can anyone help me how to make the code more accurate.

Thank you



Private Sub TwoPicksOnly(sGroup As String, sName As String)

Dim ole As OLEObject
Dim CheckedBoxCount As Integer
CheckedBoxCount = 0

For Each ole In Me.OLEObjects
If TypeName(ole.Object) = "CheckBox" And ole.Object.GroupName = sGroup And ole.Object.Value = True Then CheckedBoxCount = CheckedBoxCount + 1
If CheckedBoxCount > 2 And ole.Object.GroupName = sGroup Then ole.Object.Value = False And ole.Object.Enabled = False
Next ole

End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You need to make two iterrations thru the checkboxes - one to see how many are TRUE and the second to disable/enable as required. Recommend using the click event for the checkbox so as it is check/unchecked the validation is performed.

Code:
Private Sub CheckBox1_Click()
   TwoPicksOnly CheckBox1
End Sub
Private Sub CheckBox2_Click()
    TwoPicksOnly CheckBox2
End Sub
' Continue with these as needed

Private Sub TwoPicksOnly(chkbox)
    Dim ole As OLEObject
    Dim CheckedBoxCount As Integer
    Dim sGroup As String
    CheckedBoxCount = 0
    sGroup = chkbox.GroupName
    For Each ole In Me.OLEObjects
        If TypeName(ole.Object) = "CheckBox" Then
            If ole.Object.GroupName = sGroup _
                And ole.Object.Value = True Then
                    CheckedBoxCount = CheckedBoxCount + 1
            End If
        End If
    Next ole
    
    For Each ole In Me.OLEObjects
        If TypeName(ole.Object) = "CheckBox" Then
            If ole.Object.GroupName = sGroup _
                And ole.Object.Value = False Then
                    If CheckedBoxCount = 2 Then
                        ole.Object.Enabled = False
                    Else
                        ole.Object.Enabled = True
                    End If
            End If
        End If
    Next ole
End Sub

HTH,
~ Jim
 
Upvote 0

Forum statistics

Threads
1,214,386
Messages
6,119,217
Members
448,876
Latest member
Solitario

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top