VBA Autofilter / custom filter with contains variable

Kinjal Doshi

New Member
Joined
Jun 28, 2018
Messages
17
Hi,

It is regarding to one of the earlier posts.. the Code given there works fine.

but is it possible to tweak the code as per my need?
In this code.. it does filter the words that I enter with comma separated value.. but it also filters if only one of the word is available in that cell.. I want it to filter only those cells where both the words are found in same cell

1614426445103.png


Now above is my list... If I filter with "APPLE, KIWI" or "KIWI, APPLE" then it will also filter Sr. No 4, 5 & 6 where only one of the strings are given. I want it to filter only if both the criteria's match in same cell

Please check if it is possible
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
something like this (not tested)

VBA Code:
Sub Test()
    Dim r As Range, f As Range, cell As String, iTxt As Variant, arr() As Variant, n As Long, a As Variant
    
    Set r = Selection.CurrentRegion
    r.AutoFilter
    iTxt = InputBox("containing : t, t …", Default:=1)
    n = 0
    For Each a In Split(iTxt, ",")
        Set f = r.Find(a, LookIn:=xlValues, lookat:=xlPart)
        If Not f Is Nothing Then
            cell = f.Address
            Do
                ReDim Preserve arr(n)
                x = 0
                y = 0
                For Each b In Split(iTxt, ",")
                    If Worksheet.Match(b, f.Value, 0) > 0 Then x = x + 1
                    y = y + 1
                Next
                If x = y Then
                    arr(n) = f.Value
                    n = n + 1
                End If
                Set f = r.FindNext(f)
            Loop While Not f Is Nothing And f.Address <> cell
        End If
    Next
    r.AutoFilter Field:=1, Criteria1:=arr, Operator:=xlFilterValues
End Sub
 
Upvote 0
Solution
when tested I found a mistake: If Worksheet.Match(b, f.Value, 0) > 0 Then x = x + 1 --> If InStr(f, b) > 0 Then x = x + 1
 
Upvote 0

Forum statistics

Threads
1,215,852
Messages
6,127,322
Members
449,374
Latest member
analystvar

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