Filtering multiple columns w/ Multiple criteria

cspengel

Board Regular
Joined
Oct 29, 2022
Messages
173
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello, I have searched this forum and google and couldn't exactly find what I am looking for. I feel like I asked this a year or so ago and trying to re-create a workbook I lost.

I am using a macro from this page: All Combinations of Multiple Columns Without Duplicates

My issue is filtering my data has become a massive headache as changing the filter in one column removes the information needed in another column.

i.e say I was searching for the name David in column B, but david is also in column C. I then lost that entry. The information cannot simply be filtered using advanced filter as the information i want to filter is ever changing and I may not know what I want to filter until I see the name.
Is it possible to have a drop down list added automatically that contains all the names (no duplicates) in my dataset. Then i just click on the names and the set will filter?

If you look at my image (I have my helper columns showing), I want the following to occur:

-if i select one or more names to filter, all rows containing selected names(regardless of column) will appear. Thanks for your help!
 

Attachments

  • Untitled.png
    Untitled.png
    130.2 KB · Views: 29

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Anyone? If I recall maybe something with a combo box and a list of names. If that's the case, how would I sort the row vba to filter multiple names. Thanks!
 
Upvote 0
You'd probably get a better response if you provided a sample of your data with the XL2BB add in so we wouldn't have to type in/create a data set to test the code. With that in mind, I've provided a demonstration below that does what I think you are looking for. Obviously you'll need to adjust references/sheet names etc. It is provided for demonstration purposes only. Credit to TrumpExcel for the worksheet_change code.

With this on sheet1
cspengel.xlsm
ABCD
1Name1Name2Name3Name4
2AdamBillHelenSteve
3BillChrisIreneTracy
4ChrisDavidBillUrsula
5DavidEricChrisVanessa
6EricFrancisDavidBill
7FrancisGillianEricChris
8GillianHelenFrancisDavid
9HelenIreneGillianEric
10IreneJackieHelenFrancis
11JackieAdamIreneGillian
Sheet1


And this on sheet2
cspengel.xlsm
CDE
1DropdownUnique Names List
2AdamAdam
3Bill
4Chris
5David
6Eric
7Francis
8Gillian
9Helen
10Irene
11Jackie
12Steve
13Tracy
14Ursula
15Vanessa
16
Sheet2
Cell Formulas
RangeFormula
E2:E15E2=SORT(UNIQUE(VSTACK(Sheet1!A2:A11,Sheet1!B2:B11,Sheet1!C2:C11,Sheet1!D2:D11)))
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
C2List=$E$2#


With this worksheet_change code for sheet2
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To allow multiple selections in a Drop Down List in Excel (without repetition)
    Dim Oldvalue As String
    Dim Newvalue As String
    Application.EnableEvents = True
    On Error GoTo Exitsub
    If Target.Address = "$C$2" Then
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            GoTo Exitsub
            Else
            If Target.Value = "" Then GoTo Exitsub Else
            Application.EnableEvents = False
            Newvalue = Target.Value
            Application.Undo
            Oldvalue = Target.Value
            If Oldvalue = "" Then
                Target.Value = Newvalue
                Else
                If InStr(1, Oldvalue, Newvalue) = 0 Then
                    Target.Value = Oldvalue & " " & Newvalue
                Else
                Target.Value = Oldvalue
                End If
            End If
        End If
    End If
Exitsub:
    Application.EnableEvents = True
End Sub

When you run this code in a standard module
VBA Code:
Option Explicit
Sub cspengel()
    Application.ScreenUpdating = False
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Worksheets("Sheet1")
    Set Ws2 = Worksheets("Sheet2")
    Dim LRow As Long, i As Long, j As Long
    
    Dim MyName As String, x As String, Arr
    MyName = Ws2.Range("C2")
    If MyName <> "" Then
        Arr = Split(MyName)
        
        Ws1.Cells.EntireRow.Hidden = False
        LRow = Ws1.Cells(Rows.Count, 1).End(3).Row
        Ws1.Range(Ws1.Cells(2, 1), Ws1.Cells(LRow, 1)).EntireRow.Hidden = True
        
        For i = LBound(Arr) To UBound(Arr)
            x = WorksheetFunction.Trim(Arr(i))
            For j = 2 To LRow
                If WorksheetFunction.CountIf(Ws1.Cells(j, 1).EntireRow, x) > 0 Then Ws1.Cells(j, 1).EntireRow.Hidden = False
            Next j
        Next i
    End If
    Application.ScreenUpdating = True
End Sub

You will get this result when you select "Adam" in the dropdown on sheet2 cell C2
cspengel.xlsm
ABCD
1Name1Name2Name3Name4
2AdamBillHelenSteve
11JackieAdamIreneGillian
12
Sheet1


You can also select multiple names in the dropdown on sheet2 cell C2, like this:
cspengel.xlsm
CDE
1DropdownUnique Names List
2Adam ChrisAdam
3Bill
4Chris
5David
6Eric
7Francis
8Gillian
9Helen
10Irene
11Jackie
12Steve
13Tracy
14Ursula
15Vanessa
16
Sheet2
Cell Formulas
RangeFormula
E2:E15E2=SORT(UNIQUE(VSTACK(Sheet1!A2:A11,Sheet1!B2:B11,Sheet1!C2:C11,Sheet1!D2:D11)))
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
C2List=$E$2#


When you run the code, you get the following on sheet1
cspengel.xlsm
ABCD
1Name1Name2Name3Name4
2AdamBillHelenSteve
3BillChrisIreneTracy
4ChrisDavidBillUrsula
5DavidEricChrisVanessa
7FrancisGillianEricChris
11JackieAdamIreneGillian
12
Sheet1


I think that's the sort of thing you were looking for?
 
Upvote 0
Solution
You'd probably get a better response if you provided a sample of your data with the XL2BB add in so we wouldn't have to type in/create a data set to test the code. With that in mind, I've provided a demonstration below that does what I think you are looking for. Obviously you'll need to adjust references/sheet names etc. It is provided for demonstration purposes only. Credit to TrumpExcel for the worksheet_change code.

With this on sheet1
cspengel.xlsm
ABCD
1Name1Name2Name3Name4
2AdamBillHelenSteve
3BillChrisIreneTracy
4ChrisDavidBillUrsula
5DavidEricChrisVanessa
6EricFrancisDavidBill
7FrancisGillianEricChris
8GillianHelenFrancisDavid
9HelenIreneGillianEric
10IreneJackieHelenFrancis
11JackieAdamIreneGillian
Sheet1


And this on sheet2
cspengel.xlsm
CDE
1DropdownUnique Names List
2AdamAdam
3Bill
4Chris
5David
6Eric
7Francis
8Gillian
9Helen
10Irene
11Jackie
12Steve
13Tracy
14Ursula
15Vanessa
16
Sheet2
Cell Formulas
RangeFormula
E2:E15E2=SORT(UNIQUE(VSTACK(Sheet1!A2:A11,Sheet1!B2:B11,Sheet1!C2:C11,Sheet1!D2:D11)))
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
C2List=$E$2#


With this worksheet_change code for sheet2
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Code by Sumit Bansal from https://trumpexcel.com
' To allow multiple selections in a Drop Down List in Excel (without repetition)
    Dim Oldvalue As String
    Dim Newvalue As String
    Application.EnableEvents = True
    On Error GoTo Exitsub
    If Target.Address = "$C$2" Then
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            GoTo Exitsub
            Else
            If Target.Value = "" Then GoTo Exitsub Else
            Application.EnableEvents = False
            Newvalue = Target.Value
            Application.Undo
            Oldvalue = Target.Value
            If Oldvalue = "" Then
                Target.Value = Newvalue
                Else
                If InStr(1, Oldvalue, Newvalue) = 0 Then
                    Target.Value = Oldvalue & " " & Newvalue
                Else
                Target.Value = Oldvalue
                End If
            End If
        End If
    End If
Exitsub:
    Application.EnableEvents = True
End Sub

When you run this code in a standard module
VBA Code:
Option Explicit
Sub cspengel()
    Application.ScreenUpdating = False
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Worksheets("Sheet1")
    Set Ws2 = Worksheets("Sheet2")
    Dim LRow As Long, i As Long, j As Long
   
    Dim MyName As String, x As String, Arr
    MyName = Ws2.Range("C2")
    If MyName <> "" Then
        Arr = Split(MyName)
       
        Ws1.Cells.EntireRow.Hidden = False
        LRow = Ws1.Cells(Rows.Count, 1).End(3).Row
        Ws1.Range(Ws1.Cells(2, 1), Ws1.Cells(LRow, 1)).EntireRow.Hidden = True
       
        For i = LBound(Arr) To UBound(Arr)
            x = WorksheetFunction.Trim(Arr(i))
            For j = 2 To LRow
                If WorksheetFunction.CountIf(Ws1.Cells(j, 1).EntireRow, x) > 0 Then Ws1.Cells(j, 1).EntireRow.Hidden = False
            Next j
        Next i
    End If
    Application.ScreenUpdating = True
End Sub

You will get this result when you select "Adam" in the dropdown on sheet2 cell C2
cspengel.xlsm
ABCD
1Name1Name2Name3Name4
2AdamBillHelenSteve
11JackieAdamIreneGillian
12
Sheet1


You can also select multiple names in the dropdown on sheet2 cell C2, like this:
cspengel.xlsm
CDE
1DropdownUnique Names List
2Adam ChrisAdam
3Bill
4Chris
5David
6Eric
7Francis
8Gillian
9Helen
10Irene
11Jackie
12Steve
13Tracy
14Ursula
15Vanessa
16
Sheet2
Cell Formulas
RangeFormula
E2:E15E2=SORT(UNIQUE(VSTACK(Sheet1!A2:A11,Sheet1!B2:B11,Sheet1!C2:C11,Sheet1!D2:D11)))
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
C2List=$E$2#


When you run the code, you get the following on sheet1
cspengel.xlsm
ABCD
1Name1Name2Name3Name4
2AdamBillHelenSteve
3BillChrisIreneTracy
4ChrisDavidBillUrsula
5DavidEricChrisVanessa
7FrancisGillianEricChris
11JackieAdamIreneGillian
12
Sheet1


I think that's the sort of thing you were looking for?
Really appreciate your time with this. Was losing my mind and Been searching for days for a solution. In the future I will add a workbook my apologies.

With the code your provided, is it possible to to know which rows contain multiple names I select from drop list . So if I I chose Adam and Chris, could I apply some sort of color conditioning to differentiate which rows contain 1 name and which contain multiple? Sorry if that's confusing .
 
Upvote 0
Try the following - just change the colors to whatever you want.

VBA Code:
Option Explicit
Sub cspengel_2()
    Application.ScreenUpdating = False
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Worksheets("Sheet1")
    Set Ws2 = Worksheets("Sheet2")
    Dim LRow As Long, LCol As Long, i As Long, j As Long, k As Long
    
    Dim MyName As String, x As String, Arr
    MyName = Ws2.Range("C2")
    If MyName <> "" Then
        Arr = Split(MyName)
        
        Ws1.Cells.EntireRow.Hidden = False
        Ws1.UsedRange.Offset(1).Interior.Color = xlNone
        LRow = Ws1.Cells(Rows.Count, 1).End(3).Row
        LCol = Ws1.Cells(1, Columns.Count).End(xlToLeft).Column
        Ws1.Range(Ws1.Cells(2, 1), Ws1.Cells(LRow, 1)).EntireRow.Hidden = True
        
        For i = LBound(Arr) To UBound(Arr)
            x = WorksheetFunction.Trim(Arr(i))
            For j = 2 To LRow
                If WorksheetFunction.CountIf(Ws1.Cells(j, 1).EntireRow, x) > 0 Then Ws1.Cells(j, 1).EntireRow.Hidden = False
            Next j
        Next i
        
        For j = 2 To LRow
            For i = LBound(Arr) To UBound(Arr)
            x = WorksheetFunction.Trim(Arr(i))
                If WorksheetFunction.CountIf(Ws1.Cells(j, 1).EntireRow, x) > 0 Then k = k + 1
            Next i
            With Ws1.Range(Ws1.Cells(j, 1), Ws1.Cells(j, LCol)).Interior
                If k = 1 Then
                    .ThemeColor = xlThemeColorAccent4
                    .TintAndShade = 0.799981688894314
                ElseIf k > 1 Then
                    .ThemeColor = xlThemeColorAccent5
                    .TintAndShade = 0.799981688894314
                End If
            End With
            k = 0
        Next j
        
    End If
    Application.ScreenUpdating = True
End Sub

Leads to this result (using your example scenario)

cspengel.xlsm
ABCD
1Name1Name2Name3Name4
2AdamBillHelenSteve
3BillChrisAdamTracy
4ChrisDavidBillUrsula
5DavidEricChrisVanessa
7AdamGillianEricChris
11JackieAdamIreneGillian
12
Sheet1
 
Upvote 0
Try the following - just change the colors to whatever you want.

VBA Code:
Option Explicit
Sub cspengel_2()
    Application.ScreenUpdating = False
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Worksheets("Sheet1")
    Set Ws2 = Worksheets("Sheet2")
    Dim LRow As Long, LCol As Long, i As Long, j As Long, k As Long
   
    Dim MyName As String, x As String, Arr
    MyName = Ws2.Range("C2")
    If MyName <> "" Then
        Arr = Split(MyName)
       
        Ws1.Cells.EntireRow.Hidden = False
        Ws1.UsedRange.Offset(1).Interior.Color = xlNone
        LRow = Ws1.Cells(Rows.Count, 1).End(3).Row
        LCol = Ws1.Cells(1, Columns.Count).End(xlToLeft).Column
        Ws1.Range(Ws1.Cells(2, 1), Ws1.Cells(LRow, 1)).EntireRow.Hidden = True
       
        For i = LBound(Arr) To UBound(Arr)
            x = WorksheetFunction.Trim(Arr(i))
            For j = 2 To LRow
                If WorksheetFunction.CountIf(Ws1.Cells(j, 1).EntireRow, x) > 0 Then Ws1.Cells(j, 1).EntireRow.Hidden = False
            Next j
        Next i
       
        For j = 2 To LRow
            For i = LBound(Arr) To UBound(Arr)
            x = WorksheetFunction.Trim(Arr(i))
                If WorksheetFunction.CountIf(Ws1.Cells(j, 1).EntireRow, x) > 0 Then k = k + 1
            Next i
            With Ws1.Range(Ws1.Cells(j, 1), Ws1.Cells(j, LCol)).Interior
                If k = 1 Then
                    .ThemeColor = xlThemeColorAccent4
                    .TintAndShade = 0.799981688894314
                ElseIf k > 1 Then
                    .ThemeColor = xlThemeColorAccent5
                    .TintAndShade = 0.799981688894314
                End If
            End With
            k = 0
        Next j
       
    End If
    Application.ScreenUpdating = True
End Sub

Leads to this result (using your example scenario)

cspengel.xlsm
ABCD
1Name1Name2Name3Name4
2AdamBillHelenSteve
3BillChrisAdamTracy
4ChrisDavidBillUrsula
5DavidEricChrisVanessa
7AdamGillianEricChris
11JackieAdamIreneGillian
12
Sheet1
I finally got around to trying to implement this code into my worksheet. Been a major headache as I clearly don't know what I am doing. As for the names portion of the code, I created a new sheet just to try it out. I can get it to work if the names are first names only. As soon as I add last names, it filters everything away instead of the selection I chose.

I've also been struggling to understand portions of the code to try and get it to the actual columns I want. (as what I am trying to filter is K:S
 
Upvote 0
No worries 😊
You’ll need to make a number of changes from the solution in post #6. Given the following data on sheet 1:

cspengel.xlsm
KLMNOPQRS
1Name1Name2Name3Name4Name5Name6Name7Name8Name9
2Jalen HurtsDalvin CookCooper KuppA.J.BrownCooper KuppKenneth Walker IIIDalvin CookTony PollardJustin Jefferson
3A.J.BrownJalen HurtsSaquon BarkleyA.J.BrownCooper KuppDavante AdamsDerrick HenrySaquon BarkleyJustin Jefferson
4Dalvin CookTony PollardDavante AdamsA.J.BrownCooper KuppJustin JeffersonDavante AdamsSaquon BarkleyDavante Adams
5Kenneth Walker IIIDalvin CookDavante AdamsDeVonta SmithCooper KuppJustin JeffersonKenneth Walker IIIKenneth Walker IIITony Pollard
6Kenneth Walker IIIDeVonta SmithDavante AdamsDeVonta SmithCooper KuppDeVonta SmithKenneth Walker IIIKenneth Walker IIIDerrick Henry
7Justin JeffersonDerrick HenryJalen HurtsDalvin CookCooper KuppTony PollardKenneth Walker IIIKenneth Walker IIIDeVonta Smith
8Davante AdamsDerrick HenryJalen HurtsDalvin CookCooper KuppTony PollardDeVonta SmithDerrick HenryDeVonta Smith
9DeVonta SmithDerrick HenryJalen HurtsDalvin CookCooper KuppSaquon BarkleyDavante AdamsDeVonta SmithDalvin Cook
10Saquon BarkleyDeVonta SmithDavante AdamsDeVonta SmithCooper KuppA.J.BrownJustin JeffersonSaquon BarkleyA.J.Brown
11Tony PollardTony PollardTony PollardTony PollardCooper KuppA.J.BrownJustin JeffersonSaquon BarkleyA.J.Brown
12
Sheet1


And this layout on sheet 2 (note the change to the formula used to get the unique list in cell E2 (change the last parameter 1000 to enough rows to capture all your data) – also, make the alignment format for cell E2 ‘wrap text’):

cspengel.xlsm
CDE
1DropdownUnique Names List
2Derrick Henry; Jalen HurtsA.J.Brown
3Cooper Kupp
4Dalvin Cook
5Davante Adams
6Derrick Henry
7DeVonta Smith
8Jalen Hurts
9Justin Jefferson
10Kenneth Walker III
11Saquon Barkley
12Tony Pollard
13
Sheet2
Cell Formulas
RangeFormula
E2:E12E2=SORT(UNIQUE(TOCOL(Sheet1!K2:S1000,3)))
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
C2List=$E$2#


With this worksheet_change code (note the addition of the semi-colon) for sheet 2:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Oldvalue As String
    Dim Newvalue As String
    Application.EnableEvents = True
    On Error GoTo Exitsub
    If Target.Address = "$C$2" Then
        If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
            GoTo Exitsub
            Else
            If Target.Value = "" Then GoTo Exitsub Else
            Application.EnableEvents = False
            Newvalue = Target.Value
            Application.Undo
            Oldvalue = Target.Value
            If Oldvalue = "" Then
                Target.Value = Newvalue
                Else
                If InStr(1, Oldvalue, Newvalue) = 0 Then
                    Target.Value = Oldvalue & "; " & Newvalue
                Else
                Target.Value = Oldvalue
                End If
            End If
        End If
    End If
Exitsub:
    Application.EnableEvents = True
End Sub

And this amended sub:

VBA Code:
Option Explicit
Sub cspengel_3()
    Application.ScreenUpdating = False
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Worksheets("Sheet1")
    Set Ws2 = Worksheets("Sheet2")
    Dim LRow As Long, LCol As Long, i As Long, j As Long, k As Long
   
    Dim MyName As String, x As String, Arr
    MyName = Ws2.Range("C2")
    If MyName <> "" Then
        Arr = Split(MyName, ";")
       
        Ws1.Cells.EntireRow.Hidden = False
        Ws1.UsedRange.Offset(1).Interior.Color = xlNone
        LRow = Ws1.Cells.Find("*", , xlFormulas, , 1, 2).Row
        LCol = Ws1.Cells(1, Columns.Count).End(xlToLeft).Column
        Ws1.Range(Ws1.Cells(2, 1), Ws1.Cells(LRow, 1)).EntireRow.Hidden = True
       
        For i = LBound(Arr) To UBound(Arr)
            x = WorksheetFunction.Trim(Arr(i))
            For j = 2 To LRow
                If WorksheetFunction.CountIf(Ws1.Cells(j, 1).EntireRow, x) > 0 Then Ws1.Cells(j, 1).EntireRow.Hidden = False
            Next j
        Next i
       
        For j = 2 To LRow
            For i = LBound(Arr) To UBound(Arr)
            x = WorksheetFunction.Trim(Arr(i))
                If WorksheetFunction.CountIf(Ws1.Cells(j, 1).EntireRow, x) > 0 Then k = k + 1
            Next i
            With Ws1.Range(Ws1.Cells(j, 11), Ws1.Cells(j, LCol)).Interior
                If k = 1 Then
                    .ThemeColor = xlThemeColorAccent4
                    .TintAndShade = 0.799981688894314
                ElseIf k > 1 Then
                    .ThemeColor = xlThemeColorAccent5
                    .TintAndShade = 0.799981688894314
                End If
            End With
            k = 0
        Next j
       
    End If
    Application.ScreenUpdating = True
End Sub

You’ll get this result:

cspengel.xlsm
KLMNOPQRS
1Name1Name2Name3Name4Name5Name6Name7Name8Name9
2Jalen HurtsDalvin CookCooper KuppA.J.BrownCooper KuppKenneth Walker IIIDalvin CookTony PollardJustin Jefferson
3A.J.BrownJalen HurtsSaquon BarkleyA.J.BrownCooper KuppDavante AdamsDerrick HenrySaquon BarkleyJustin Jefferson
6Kenneth Walker IIIDeVonta SmithDavante AdamsDeVonta SmithCooper KuppDeVonta SmithKenneth Walker IIIKenneth Walker IIIDerrick Henry
7Justin JeffersonDerrick HenryJalen HurtsDalvin CookCooper KuppTony PollardKenneth Walker IIIKenneth Walker IIIDeVonta Smith
8Davante AdamsDerrick HenryJalen HurtsDalvin CookCooper KuppTony PollardDeVonta SmithDerrick HenryDeVonta Smith
9DeVonta SmithDerrick HenryJalen HurtsDalvin CookCooper KuppSaquon BarkleyDavante AdamsDeVonta SmithDalvin Cook
12
Sheet1
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,895
Members
449,097
Latest member
dbomb1414

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