Filter Checkbox.Value

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
834
Hello, trying to use a checkbox to filter a sheet. The CheckBox3.Caption works, but when I add further filters, CheckBox4 and CheckBox5 it doesn't; any ideas please?

VBA Code:
Private Sub CommandButton1_Click()

If OptionButton1.Value And OptionButton3.Value = True Then

Lastrow = Sheets("Data - Accidents").Cells(Rows.Count, "A").End(xlUp).Row

'Application.Visible = False

With Sheets("Data - Accidents")
            .Range("A1:AS" & Lastrow).AutoFilter Field:=8, Criteria1:="Employee"
            .Range("A1:AS" & Lastrow).AutoFilter Field:=45, Criteria1:=TextBox2
            If CheckBox3.Value = True Then
            .Range("A1:AS" & Lastrow).AutoFilter Field:=39, Criteria1:=CheckBox3.Caption
            If CheckBox4.Value = True Then
            .Range("A1:AS" & Lastrow).AutoFilter Field:=39, Criteria1:=CheckBox4.Caption
            If CheckBox5.Value = True Then
            .Range("A1:AS" & Lastrow).AutoFilter Field:=39, Criteria1:=CheckBox5.Caption

End If

End If

End If

End With

End If

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try stepping through the code with F8, that way you will see that if Ceckbox3 is false it never looks at the other two checkboxes.
Also (IMO) it is easier to see the "flow" of the code if properly indented, like
VBA Code:
      With Sheets("Data - Accidents")
         .Range("A1:AS" & LastRow).AutoFilter Field:=8, Criteria1:="Employee"
         .Range("A1:AS" & LastRow).AutoFilter Field:=45, Criteria1:=TextBox2
         If CheckBox3.Value = True Then
            .Range("A1:AS" & LastRow).AutoFilter Field:=39, Criteria1:=CheckBox3.Caption
            If CheckBox4.Value = True Then
               .Range("A1:AS" & LastRow).AutoFilter Field:=39, Criteria1:=CheckBox4.Caption
               If CheckBox5.Value = True Then
                  .Range("A1:AS" & LastRow).AutoFilter Field:=39, Criteria1:=CheckBox5.Caption
               End If
            End If
         End If
      End With
That said, will you only ever have one checkbox selected?
 
Upvote 0
Ok, how about
VBA Code:
Private Sub CommandButton1_Click()
   Dim Ary(1 To 3) As Variant
   Dim i As Long
   Dim Flg As Boolean
   
   If OptionButton1.Value And OptionButton3.Value = True Then
      For i = 1 To 3
         If Me.Controls("CheckBox" & i) Then
            Ary(i) = Me.Controls("Checkbox" & i).Caption
            Flg = True
         End If
      Next i
      LastRow = Sheets("Data - Accidents").Cells(Rows.Count, "A").End(xlUp).Row
      
      'Application.Visible = False
      With Sheets("Data - Accidents")
         .Range("A1:AS" & LastRow).AutoFilter Field:=8, Criteria1:="cumbria" '"Employee"
         .Range("A1:AS" & LastRow).AutoFilter Field:=45, Criteria1:=TextBox2
         If Flg Then
            .Range("A1:AS" & LastRow).AutoFilter 39, Ary, xlFilterValues
         Else
            .Range("A1:AS" & LastRow).AutoFilter 39
         End If
      End With
   End If

End Sub
 
Upvote 0
Failing on this code: "Could not find the specified object."

VBA Code:
         If Me.Controls("CheckBox" & i) Then
 
Upvote 0
Oops, your checkboxes are 3 to 5 not 1 to 3. Try
VBA Code:
      For i = 3 To 5
         If Me.Controls("CheckBox" & i) Then
            Ary(i - 2) = Me.Controls("Checkbox" & i).Caption
            Flg = True
         End If
      Next i
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,454
Members
448,898
Latest member
drewmorgan128

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