Filter multiple columns for two words

tinejf

New Member
Joined
Feb 21, 2023
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi,
I need help to filter multiple columns for two words at the same time. I have added and example. Here are the info:

There is a heading on row 1 and 2, and other information on in column A to F. The columns that I want to filter is between G and N and rows down is undefined. Columns after S is empty.
Here is a table to use as an example:
NameCompanyNotesNotes2Notes3Notes4Notes5Notes6Notes7Notes8
MikeSkySoldSoldRNDRNDRNDRNDRNDRND
JohnGlowSoldRNDSoldSoldBoughtBoughtRNDSold
CharlesLinkBoughtBoughtBoughtBoughtTBNTBNTBNBought
KarenHeroRNDSoldBoughtBoughtBoughtBoughtBoughtSold
DennisLetgoTBNTBNTBNBoughtBoughtBoughtBoughtTBN
AngelaGreatsoBoughtSoldBoughtBoughtBoughtBoughtSoldBought
PaulMarshmellowSoldBoughtSoldSoldSoldSoldRNDRND

I want to filter all the rows containing "RND" or "Sold" or both in columns G to N, and hide the rows not containing "RND" and "Sold".

I previously recieved this macro code from @Peter_SSs which worked well (ref: Filter multiple columns for a single word) but I can't figure out how to use it for multiple words:

VBA Code:
Sub Show_My_Rows_of_Interest()
  Dim rCrit As Range
 
  Const TextOfInterest As String = "RND"
  Const ColsOfInterest As String = "F:N"
 
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  With ActiveSheet
    On Error Resume Next
    .ShowAllData
    On Error GoTo 0
    Set rCrit = .Range("Z1:Z2")
    With Intersect(.UsedRange, .Columns(ColsOfInterest))
      rCrit.Cells(2).Formula = Replace(Replace("=COUNTIF(#,""*%*"")", "#", .Rows(2).Address(0, 0)), "%", TextOfInterest)
      .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rCrit, Unique:=False
    End With
    rCrit.Cells(2).ClearContents
  End With
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub

Thank you, in advance!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I'm a bit unclear as to the actual layout of your data - it would be so much simpler if you could provide a sample of your sheet using the XL2BB add in. Having said that, try the following code on a copy of your data. The before & after results are posted below. Is this what you're after?

VBA Code:
Option Explicit
Sub Multi_Column_Filter()
    Dim ws As Worksheet, LCol As Long, LRow As Long
    Set ws = Worksheets("Sheet1")           '<<~~ Change to actual sheet name ***
    
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    
    Dim a, b, i As Long, j As Long
    a = ws.Range(ws.Cells(3, 7), ws.Cells(LRow, LCol - 1))
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a, 1)
        For j = 1 To UBound(a, 2)
            If a(i, j) = "RND" Or a(i, j) = "Sold" Then b(i, 1) = 1
        Next j
    Next i
    ws.Cells(3, LCol).Resize(UBound(b, 1)).Value2 = b
    
    Dim Rdata As Range, Rcrit As Range
    Set Rdata = ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol - 1)).Resize(, LCol)
    Set Rcrit = ws.Range(ws.Cells(2, LCol + 1), ws.Cells(3, LCol + 1))
    Rcrit.Cells(1).Offset(, -1).Resize(, 2).Value2 = "X"
    Rcrit.Cells(2) = 1
    
    With Rdata
        .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Rcrit, Unique:=False
    End With
    
    ws.Columns(LCol).Resize(, 2).EntireColumn.ClearContents
End Sub

Before:
Multi Filter.xlsm
ABCDEFGHIJKLMNOPQRST
1
2NameCompanyNotesNotes2Notes3Notes4Notes5Notes6Notes7Notes8Notes9Notes10Notes11Notes12Notes13Notes14Notes15Notes16Notes17
3MikeSkySoldSoldRNDRNDRNDRNDRNDRNDSoldRNDRNDRNDRNDRNDRNDSoldRND
4JohnGlowSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSold
5CharlesLinkBoughtBoughtBoughtBoughtTBNTBNTBNBoughtBoughtBoughtBoughtTBNTBNTBNBoughtBoughtBought
6KarenHeroRNDSoldBoughtBoughtBoughtBoughtBoughtSoldSoldBoughtBoughtBoughtBoughtBoughtSoldSoldBought
7DennisLetgoTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNBoughtTBNTBNTBN
8AngelaGreatsoBoughtSoldBoughtBoughtBoughtBoughtSoldBoughtSoldBoughtBoughtBoughtBoughtSoldBoughtSoldBought
9PaulMarshmellowSoldBoughtSoldSoldSoldSoldRNDRNDBoughtSoldSoldSoldSoldRNDRNDBoughtSold
10
Sheet1


After:
Multi Filter.xlsm
ABCDEFGHIJKLMNOPQRST
1
2NameCompanyNotesNotes2Notes3Notes4Notes5Notes6Notes7Notes8Notes9Notes10Notes11Notes12Notes13Notes14Notes15Notes16Notes17
3MikeSkySoldSoldRNDRNDRNDRNDRNDRNDSoldRNDRNDRNDRNDRNDRNDSoldRND
4JohnGlowSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSold
6KarenHeroRNDSoldBoughtBoughtBoughtBoughtBoughtSoldSoldBoughtBoughtBoughtBoughtBoughtSoldSoldBought
8AngelaGreatsoBoughtSoldBoughtBoughtBoughtBoughtSoldBoughtSoldBoughtBoughtBoughtBoughtSoldBoughtSoldBought
9PaulMarshmellowSoldBoughtSoldSoldSoldSoldRNDRNDBoughtSoldSoldSoldSoldRNDRNDBoughtSold
10
Sheet1
 
Upvote 1
Solution
Hi @kevin9999,
This worked perfectly, thank you so much! :D

I realized that it would also be preferable to have the option of showing only the rows including both "RND" and "Sold", are you also able to help me with that?
Thank you, in advance!

- Tine
 
Upvote 0
This worked perfectly, thank you so much! :D
Happy to have helped, and thank you for the feedback (y) :)

I realized that it would also be preferable to have the option of showing only the rows including both "RND" and "Sold"
Try the following:

VBA Code:
Option Explicit
Sub Multi_Column_Filter_RND_and_Sold()
    Dim ws As Worksheet, LCol As Long, LRow As Long
    Set ws = Worksheets("Sheet1")           '<<~~ Change to actual sheet name ***
    
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    
    Dim a, b, i As Long, j As Long, k As Long, x As Long
    a = ws.Range(ws.Cells(3, 7), ws.Cells(LRow, LCol - 1))
    ReDim b(1 To UBound(a, 1), 1 To 1)
    
    For i = 1 To UBound(a, 1)
        k = 0: x = 0
        For j = 1 To UBound(a, 2)
            If a(i, j) = "RND" Then k = k + 1
            If a(i, j) = "Sold" Then x = x + 1
        Next j
        If k > 0 And x > 0 Then b(i, 1) = 1
    Next i
    
    ws.Cells(3, LCol).Resize(UBound(b, 1)).Value2 = b
    
    Dim Rdata As Range, Rcrit As Range
    Set Rdata = ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol - 1)).Resize(, LCol)
    Set Rcrit = ws.Range(ws.Cells(2, LCol + 1), ws.Cells(3, LCol + 1))
    Rcrit.Cells(1).Offset(, -1).Resize(, 2).Value2 = "X"
    Rcrit.Cells(2) = 1
    
    With Rdata
        .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Rcrit, Unique:=False
    End With
    
    ws.Columns(LCol).Resize(, 2).EntireColumn.ClearContents
End Sub

Before:
tinejf.xlsm
ABCDEFGHIJKLMNOPQRST
1
2NameCompanyNotesNotes2Notes3Notes4Notes5Notes6Notes7Notes8Notes9Notes10Notes11Notes12Notes13Notes14Notes15Notes16Notes17
3MikeSkySoldSoldRNDRNDRNDRNDRNDRNDSoldRNDRNDRNDRNDRNDRNDSoldRND
4JohnGlowSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSold
5CharlesLinkBoughtBoughtBoughtBoughtTBNTBNTBNBoughtBoughtBoughtBoughtTBNTBNTBNBoughtBoughtBought
6KarenHeroRNDSoldBoughtBoughtBoughtBoughtBoughtSoldSoldBoughtBoughtBoughtBoughtBoughtSoldSoldBought
7DennisLetgoTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNBoughtTBNTBNTBN
8AngelaGreatsoBoughtSoldBoughtBoughtBoughtBoughtSoldBoughtSoldBoughtBoughtBoughtBoughtSoldBoughtSoldBought
9PaulMarshmellowSoldBoughtSoldSoldSoldSoldRNDRNDBoughtSoldSoldSoldSoldRNDRNDBoughtSold
10
Sheet1


After:
tinejf.xlsm
ABCDEFGHIJKLMNOPQRST
1
2NameCompanyNotesNotes2Notes3Notes4Notes5Notes6Notes7Notes8Notes9Notes10Notes11Notes12Notes13Notes14Notes15Notes16Notes17
3MikeSkySoldSoldRNDRNDRNDRNDRNDRNDSoldRNDRNDRNDRNDRNDRNDSoldRND
4JohnGlowSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSold
9PaulMarshmellowSoldBoughtSoldSoldSoldSoldRNDRNDBoughtSoldSoldSoldSoldRNDRNDBoughtSold
10
Sheet1
 
Upvote 1
Happy to have helped, and thank you for the feedback (y) :)


Try the following:

VBA Code:
Option Explicit
Sub Multi_Column_Filter_RND_and_Sold()
    Dim ws As Worksheet, LCol As Long, LRow As Long
    Set ws = Worksheets("Sheet1")           '<<~~ Change to actual sheet name ***
   
    LCol = ws.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column + 1
    LRow = ws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
   
    Dim a, b, i As Long, j As Long, k As Long, x As Long
    a = ws.Range(ws.Cells(3, 7), ws.Cells(LRow, LCol - 1))
    ReDim b(1 To UBound(a, 1), 1 To 1)
   
    For i = 1 To UBound(a, 1)
        k = 0: x = 0
        For j = 1 To UBound(a, 2)
            If a(i, j) = "RND" Then k = k + 1
            If a(i, j) = "Sold" Then x = x + 1
        Next j
        If k > 0 And x > 0 Then b(i, 1) = 1
    Next i
   
    ws.Cells(3, LCol).Resize(UBound(b, 1)).Value2 = b
   
    Dim Rdata As Range, Rcrit As Range
    Set Rdata = ws.Range(ws.Cells(2, 1), ws.Cells(LRow, LCol - 1)).Resize(, LCol)
    Set Rcrit = ws.Range(ws.Cells(2, LCol + 1), ws.Cells(3, LCol + 1))
    Rcrit.Cells(1).Offset(, -1).Resize(, 2).Value2 = "X"
    Rcrit.Cells(2) = 1
   
    With Rdata
        .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Rcrit, Unique:=False
    End With
   
    ws.Columns(LCol).Resize(, 2).EntireColumn.ClearContents
End Sub

Before:
tinejf.xlsm
ABCDEFGHIJKLMNOPQRST
1
2NameCompanyNotesNotes2Notes3Notes4Notes5Notes6Notes7Notes8Notes9Notes10Notes11Notes12Notes13Notes14Notes15Notes16Notes17
3MikeSkySoldSoldRNDRNDRNDRNDRNDRNDSoldRNDRNDRNDRNDRNDRNDSoldRND
4JohnGlowSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSold
5CharlesLinkBoughtBoughtBoughtBoughtTBNTBNTBNBoughtBoughtBoughtBoughtTBNTBNTBNBoughtBoughtBought
6KarenHeroRNDSoldBoughtBoughtBoughtBoughtBoughtSoldSoldBoughtBoughtBoughtBoughtBoughtSoldSoldBought
7DennisLetgoTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNTBNBoughtTBNTBNTBN
8AngelaGreatsoBoughtSoldBoughtBoughtBoughtBoughtSoldBoughtSoldBoughtBoughtBoughtBoughtSoldBoughtSoldBought
9PaulMarshmellowSoldBoughtSoldSoldSoldSoldRNDRNDBoughtSoldSoldSoldSoldRNDRNDBoughtSold
10
Sheet1


After:
tinejf.xlsm
ABCDEFGHIJKLMNOPQRST
1
2NameCompanyNotesNotes2Notes3Notes4Notes5Notes6Notes7Notes8Notes9Notes10Notes11Notes12Notes13Notes14Notes15Notes16Notes17
3MikeSkySoldSoldRNDRNDRNDRNDRNDRNDSoldRNDRNDRNDRNDRNDRNDSoldRND
4JohnGlowSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSoldSoldBoughtBoughtRNDSoldRNDSold
9PaulMarshmellowSoldBoughtSoldSoldSoldSoldRNDRNDBoughtSoldSoldSoldSoldRNDRNDBoughtSold
10
Sheet1


Thank you for the quick reply and for the solution!
This worked perfectly aswell

- Tine
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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