+ Reply to Thread
Results 1 to 3 of 3

Autofilter with multiple criteria - how to ignore blank criteria

Hybrid View

  1. #1
    Registered User
    Join Date
    11-26-2011
    Location
    Yeovil, england
    MS-Off Ver
    365
    Posts
    83

    Autofilter with multiple criteria - how to ignore blank criteria

    Hi,

    I have the following code:

    Sub searchfilter()
    '
    ' Search page filter
    '
    '
        ActiveSheet.Range("$A$4:$K$6000").AutoFilter field:=3, Criteria1:=ActiveSheet.Range("c2")
        ActiveSheet.Range("$A$4:$K$6000").AutoFilter field:=6, Criteria1:=ActiveSheet.Range("D2")
        ActiveSheet.Range("$A$4:$K$6000").AutoFilter field:=8, Criteria1:=ActiveSheet.Range("E2")
    
    End Sub
    However it requires all cell ranges , C2, D2, and E2 to have a value to filter or it displays blank lines only. How do you adjust the above code to allow it to ignore a cell if there is no search criteria entered into it?

    Thanks

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Autofilter with multiple criteria - how to ignore blank criteria

    Hi, MOONDRIFT47,

    check the length of the cells value before filtering?

    Sub searchfilter2()
    '
    ' Search page filter
    '
    '
        With ActiveSheet
          If Len(.Range("C2").Value) > 0 Then
            .Range("$A$4:$K$6000").AutoFilter Field:=3, Criteria1:=.Range("C2")
          End If
          If Len(.Range("D2").Value) > 0 Then
            .Range("$A$4:$K$6000").AutoFilter Field:=6, Criteria1:=.Range("D2")
          End If
          If Len(.Range("E2").Value) > 0 Then
            .Range("$A$4:$K$6000").AutoFilter Field:=8, Criteria1:=.Range("E2")
          End If
        End With
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    11-26-2011
    Location
    Yeovil, england
    MS-Off Ver
    365
    Posts
    83

    Re: Autofilter with multiple criteria - how to ignore blank criteria

    Perfect, thank you very much. Happy New year!!!!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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