+ Reply to Thread
Results 1 to 3 of 3

Autofilter for multiple column

Hybrid View

  1. #1
    Registered User
    Join Date
    11-16-2023
    Location
    Singapore
    MS-Off Ver
    Microsoft 365
    Posts
    2

    Autofilter for multiple column

    Hi, I'm trying to do a filter on change in a textbox such that if it contains the value in any of 2 column it will remain in the table.
    For this example, the value user entered is a, so I would like to retain the entry as long as it has a appearing in either or both column.
    My codes are as follows but it doesn't work. It will only return results that contain the value in column A:
    ActiveSheet.ListObjects("CustData").Range.AutoFilter Field:=Array(1, 2), Criteria1:="*" & [I4] & "*", Operator:=xlFilterValues
    Value 1 Value 2 Result
    a b Retain
    b a Retain
    a a Retain
    b b Filter Out

  2. #2
    Registered User
    Join Date
    11-16-2023
    Location
    Singapore
    MS-Off Ver
    Microsoft 365
    Posts
    2

    Re: Autofilter for multiple column

    Is there no solution available?

  3. #3
    Registered User
    Join Date
    04-18-2023
    Location
    Adelaide, Australia
    MS-Off Ver
    365
    Posts
    6

    Re: Autofilter for multiple column

    It would be easier if the data was in a range, rather than a table, as you could use advanced filter to achieve what you seem to be after. Try the code below on a copy of your workbook. It assumes the criteria is in cell I4 (as per your original post). 2 codes provided. The first deals with a table, the second with a range.

    Sub If_Table()
        Dim s As String, a As Range, c As Range
        s = [I4]
        ActiveSheet.ListObjects(1).AutoFilter.ShowAllData
        Set a = ActiveSheet.ListObjects(1).DataBodyRange
        For Each c In a.Columns(1).Cells
            If c = s Or c.Offset(, 1) = s Then c.EntireRow.Hidden = False Else c.EntireRow.Hidden = True
        Next c
    End Sub
    
    Sub If_Range()
        Dim rCrit As Range
        With Range("A1").CurrentRegion
            Set rCrit = .Offset(, .Columns.Count).Resize(2, 1)
            rCrit.Cells(2).Formula = "=OR(A2=$I$4,B2= $I$4)"
            .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rCrit, Unique:=False
        End With
        rCrit.Cells(2).ClearContents
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Autofilter a column by multiple checkboxes in userform
    By impresxy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-27-2021, 08:17 AM
  2. [SOLVED] Autofilter to delete multiple rows with multiple variables
    By Ravana in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 03-10-2015, 12:01 AM
  3. [SOLVED] AutoFilter with multiple criteria in a single column
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 07-23-2014, 12:45 PM
  4. Autofilter multiple columns with multiple string criteria
    By svalentine91 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-23-2013, 11:26 AM
  5. [SOLVED] Multiple AND search on a column using Autofilter?
    By I-Like-Excel in forum Excel General
    Replies: 4
    Last Post: 04-22-2013, 02:27 AM
  6. multiple column , multiple criteria autofilter sort
    By jseufert in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-28-2009, 06:18 PM
  7. [SOLVED] Need help hiding/unhiding column based on autofilter selection in a different column
    By kcleere in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-23-2006, 02:25 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1