Reputation: 27
I am trying to filter a range in Excel, say DATA, based on if cells in another column, say CHECKBOXES are checked via a checkbox. It would look something like this:
DATA
aa
ab
ac
ba
bb
bc
ca
cb
cc
CHECKBOXES
a [x]
b [x]
c [x]
So, if 'a' were checked under the CHECKBOXES column, the DATA column would show cells that only contained the letter a, not 'b' or 'c'. But if 'a' and 'b' were checked, the DATA column would show cells that contained either the letters a OR b, but not 'c'. If all three 'a', 'b', and 'c' were checked, there would obviously not be any filtering needed.
I would like to implement this across a large range of data with both columns. Any and all help is much appreciated, Thank You.
Upvotes: 0
Views: 623
Reputation:
You cannot do this the way you've described.
Rethink your proposal and try to accomplish something towards a solution. If you run into trouble, edit your post to include new details.
if 'a' were checked under the CHECKBOXES column, the DATA column would show cells that only contained the letter a, not 'b' or 'c'. But if 'a' and 'b' were checked, the DATA column would show cells that contained either the letters a OR b, but not 'c'. If all three 'a', 'b', and 'c' were checked, there would obviously not be any filtering needed.
Here's a method that works.
Right-click the worksheet's name tab and choose View Code. Paste in the following.
Option Explicit
Sub CheckBoxABC_Click()
Dim i As Long, dict As Object, str As String
Set dict = CreateObject("scripting.dictionary")
dict.comparemode = vbTextCompare
If AutoFilterMode Then AutoFilterMode = False
With Range(Cells(1, "A"), Cells(Rows.Count, "A").End(xlUp))
For i = 2 To .Rows.Count
'store the cell value
str = .Cells(i, "A").Value
'see if it might belong and add it
If CBool(InStr(1, str, "A", vbTextCompare)) And .Parent.CheckBoxA Then _
dict.Item(str) = vbNullString
If CBool(InStr(1, str, "B", vbTextCompare)) And .Parent.CheckBoxB Then _
dict.Item(str) = vbNullString
If CBool(InStr(1, str, "C", vbTextCompare)) And .Parent.CheckBoxC Then _
dict.Item(str) = vbNullString
'see if it doesn't belong and remove it
If dict.Exists(str) And CBool(InStr(1, str, "A", vbTextCompare)) And Not .Parent.CheckBoxA Then _
dict.Remove str
If dict.Exists(str) And CBool(InStr(1, str, "B", vbTextCompare)) And Not .Parent.CheckBoxB Then _
dict.Remove str
If dict.Exists(str) And CBool(InStr(1, str, "C", vbTextCompare)) And Not .Parent.CheckBoxC Then _
dict.Remove str
Next i
If dict.Count > 0 Then
.AutoFilter field:=1, Criteria1:=dict.keys, Operator:=xlFilterValues, VisibleDropDown:=False
Else
.AutoFilter field:=1, Criteria1:="Z", VisibleDropDown:=False
End If
End With
End Sub
Private Sub CheckBoxA_Click()
CheckBoxABC_Click
End Sub
Private Sub CheckBoxB_Click()
CheckBoxABC_Click
End Sub
Private Sub CheckBoxC_Click()
CheckBoxABC_Click
End Sub
Your results should be similar to the following.
Upvotes: 0
Reputation: 2079
Well this can be done a few different ways. However, this is one way you can acomplish what you are trying to do. Create three checkboxes by using design mode from the developer tab and then simply assign 3 macros to do the 3 different buttons. You can also just create one sub and than just have that one sub(macro) do all the work. Is really up to you.
Sub OptionButton1_Click()
Range("A1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$10").AutoFilter Field:=1, Criteria1:="=a*", _
Operator:=xlAnd
End Sub
Sub OptionButton2_Click()
Range("A1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$10").AutoFilter Field:=1, Criteria1:="=b*", _
Operator:=xlAnd
End Sub
Sub option3_click()
Range("A1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$10").AutoFilter Field:=1, Criteria1:="=c*", _
Operator:=xlAnd
End Sub
Upvotes: 1