VBA Frame With Checkboxes

Noz2k

Well-known Member
Joined
Mar 15, 2011
Messages
693
I have a frame with 8 checkboxes in it. The checkboxes on in pairs (yes/no). On the outside of the frame I have an image ("X") and would like this to show unless 1 of each pair is selected, if yes is selected in pair 1. If no is selected in the first part then only pair 3 and 4 have to have a selection.

At the moment my code is this

Code:
Private Sub cbxYes3_Click()
    If cbxYes1.Value = True Then
        If cbxNo2.Value = True Or cbxYes2.Value = True _
            And cbxNo3.Value = True Or cbxYes3.Value = True _
            And cbxNo4.Value = True Or cbxYes4.Value = True Then
            imgCross21.Visible = False
        Else
            imgCross21.Visible = True
        End If
Else
    If cbxNo1.Value = True Then
        If cbxNo3.Value = True Or cbxYes3.Value = True _
        And cbxNo4.Value = True Or cbxYes4.Value = True Then
        imgCross21.Visible = False
        Else
            imgCross21.Visible = True
        End If
Else
    imgCross21.Visible = True
End If
End If
End Sub

Any help would be appreciated, thanks.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
This should help you. I've started it as generic logic, but you can turn the visibility of the other check boxes off and apply extra logic to the code if you want.


Code:
Option Explicit

Private Sub cbxYes1_Click()
    Call showX
End Sub
Private Sub cbxYes2_Click()
    Call showX
End Sub
Private Sub cbxYes3_Click()
    Call showX
End Sub
Private Sub cbxYes4_Click()
    Call showX
End Sub
Private Sub cbxNo1_Click()
    Call showX
End Sub
Private Sub cbxNo2_Click()
    Call showX
End Sub
Private Sub cbxNo3_Click()
    Call showX
End Sub
Private Sub cbxNo4_Click()
    Call showX
End Sub

Private Sub showX()
Dim ctl As Object
Dim showImage As Integer ' this is our counter - must be 8 to show image
Dim num As Integer

On Error GoTo err
    
    ' Loop through each checkbox on form
    For Each ctl In UserForm1.Controls
        If TypeOf ctl Is MSForms.CheckBox Then
            'get the number
            num = Right(ctl.Name, 1)
                           
            ' Now check the other checkbox with the same number
                If (Controls("cbxNo" & num) = False And Controls("cbxYes" & num) = True) Or _
                    (Controls("cbxNo" & num) = True And Controls("cbxYes" & num) = False) Then
                    showImage = showImage + 1
                End If
        End If
    Next ctl
        
        If showImage = 8 Then
            imgCross21.Visible = True
        Else
            imgCross21.Visible = False
        End If
        
        Exit Sub
err:
    MsgBox err.Description, vbExclamation, "Error"
        
End Sub
 
Last edited:
Upvote 0
It seemed to give me a Type Mismatch error. However I've noted a few techniques which I didn't know about, so thanks for that anyway. Am still new to vba so everything helps.

I've solved the problem a different way though, I just placed this on the frame exit event.

Code:
If cbxYes1 = True Then
    If cbxYes2 = True Or cbxNo2 = True Then
        If cbxYes3 = True Or cbxNo3 = True Then
            If cbxYes4 = True Or cbxNo4 = True Then
                imgCross21.Visible = False
            Else
                imgCross21.Visible = True
            End If
        Else
            imgCross21.Visible = True
        End If
    Else
        imgCross21.Visible = True
    End If
Else
    If cbxNo1 = True Then
        If cbxYes3 = True Or cbxNo3 = True Then
            If cbxYes4 = True Or cbxNo4 = True Then
                imgCross21.Visible = False
            Else
                imgCross21.Visible = True
            End If
        Else
            imgCross21.Visible = True
        End If
    Else
    imgCross21.Visible = True
    End If
End If
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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