Excel VBA to Filter in Same Column by Multiple Criteria: 6 Methods

Method 1 – Remove Specific Values with VBA to Filter in the Same Column by Multiple Criteria in Excel

STEPS:

  • Right-click on the worksheet tab named REMOVE.
  • Select the option ‘View Code’.

Remove Specific Values with VBA to Filter in Same Column by Multiple Criteria in Excel

  • The above action will open a blank VBA code window for that worksheet or to press Alt + F11.
  • Type the following code in that code window:
Sub Remove()
Application.DisplayAlerts = True
Worksheets("Remove").Activate
Columns("C:C").Select
    Selection.AutoFilter
    ActiveSheet.Range("$C$4:$C$15").AutoFilter Field:=1, _
    Criteria1:="<>California", Criteria2:="<>Texas", _
    Operator:=xlAnd
End Sub
  • Click on the Run or press the F5 key to run the code.

Remove Specific Values with VBA to Filter in Same Column by Multiple Criteria in Excel

  • Get results like the image below. The values California & Texas have been filtered from the column City.

 


Method 2 – Keep Particular Values in the Same Column with Excel VBA

STEPS:

  • Right-click on the worksheet tab named KEEP.
  • Click on the option ‘View Code’.

Keep Particular Values in Same Column with Excel VBA

  • It opens a blank VBA code window for the active worksheet or press Alt + F11 to get that code window.
  • Insert the following code in that code window:
Sub Keep()
Application.DisplayAlerts = True
Worksheets("KEEP").Activate
Columns("C:C").Select
Selection.AutoFilter
ActiveSheet.Range("$C$4:$C$15").AutoFilter Field:=1, _
Criteria1:=Array( _
"California", "Texas"), Operator:=xlFilterValues
End Sub
  • Run the code, click on the Run, or press the F5 Key.

Keep Particular Values in Same Column with Excel VBA

  • See the result in the below image. Only the values present in the City column are California and Texas. Other values have been filtered.


Method 3 – VBA to Filter by Multiple Criteria with Advanced Criteria Range in Same Column

STEPS:

  • Right-click on the worksheet tab name ‘Criteria_range’.
  • Click the option ‘View Code’.

VBA to Filter by Multiple Criteria with Advanced Criteria Range in Same Column

  • Get a new blank VBA code window for the active worksheet.
  • Input the following code in that code window:
Sub Advanced_Criteria()
Range("B4:D15").AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=Range("F6:G8")
End Sub
  • Press the F5 key or click on the Run to run the code.

VBA to Filter by Multiple Criteria with Advanced Criteria Range in Same Column

  • Get results like the image below. The filtered data in the image below follows our criteria range.


Method 4 – Filter in the Same Column Using VBA with OR Criteria in Excel

STEPS:

  • Right-click on the active sheet tab named OR.
  • Select the option ‘View Code’.

Filter in Same Column Using VBA with OR Criteria in Excel

  • The above command opens a blank VBA code window for that worksheet or press Alt + 11 to open that code window.
  • Type the following code in the blank code window:
Sub OR_Criteria()
Worksheets("OR").Range("B4").AutoFilter Field:=2, _
Criteria1:="New York", Operator:=xlOr, Criteria2:=">3000"
End Sub
  • Run the code, click on the Run, or press the F5.

Filter in Same Column Using VBA with OR Criteria in Excel

  • Get our dataset filtered for the defined criteria range.

 


Method 5 – Insert AND Criteria with Excel VBA to Filter in the Same Column

STEPS:

  • Right-click on the active sheet named AND.
  • Click on the option ‘View Code’.

Insert AND Criteria with Excel VBA to Filter in Same Column

  • A new blank VBA code window will open for the active worksheet or press Alt + 11.
  • Copy the following code in that code window:
Sub AND_Criteria()
Worksheets("AND").Range("B4").AutoFilter Field:=3, _
Criteria1:=">2000", Operator:=xlAnd, Criteria2:="<3500"
End Sub
  • Click Run or hit the F5 key to run the code.

Insert AND Criteria with Excel VBA to Filter in Same Column

  • See the result for filtered data in the image below.


Method 6 – Put VBA to Filter by Multiple Criteria in the Same Column with Data Range

STEPS:

  • Go to the active worksheet tab named DateRange and right-click.
  • Select the option ‘View Code’.

Put VBA to Filter by Multiple Criteria in Same Column with Data Range

  • It opens a new blank VBA code window for that worksheet or hit Alt + 11 to get that code window.
  • Type the following code in that code window:
Sub Date_Range()
Worksheets("DateRange").Range("B4:D15").AutoFilter Field:=1, _
Criteria1:=">=12-03-21", _
Operator:=xlAnd, _
Criteria2:="<=12-08-21"
End Sub
  • Run the code, click on Run, or press the F5 key.

Put VBA to Filter by Multiple Criteria in Same Column with Data Range

  • Get the Date column filtered by our given criteria.

 


Excel VBA to Turn off AutoFilter

STEPS:

  • Right-click on the active worksheet.
  • Select the option ‘View Code’.
  • A blank VBA code window will appear for that worksheet.
  • Copy the following code in that code window:
Sub Turn_Off_Filter()
Worksheets("TurnOff").AutoFilterMode = False
End Sub
  • Click Run or press the F5 key to run the code.

Excel VBA to Turn off AutoFilter

  • See the result in the following image. Our dataset no longer has any filters.


Turn on AutoFilter in Excel Using VBA

STEPS:

  • Select the active worksheet tab named ‘TurnOn’ and right-click on it.
  • Select the option ‘View Code’.

Turn on AutoFilter in Excel Using VBA

  • It will return a blank VBA code window.
  • Input the following code in the code window:
Sub Turn_On_Filter()
If Not Worksheets("TurnOn").Range("B4").AutoFilter Then
Worksheets("TurnOn").Range("B4").AutoFilter
End If
End Sub
  • Press the F5 key or click on the Run to run the code.

Turn on AutoFilter in Excel Using VBA

  • See filter icons in the header cells of our dataset.

 


Check If the Filter Is Applied or Not

STEPS:

  • Right-click on the active worksheet tab.
  • Select the option ‘View Code’.
  • A blank VBA code window will open for that worksheet or press Alt + F11.
  • Type the following code in that code window:
Sub Filter_Check()
If ActiveSheet.AutoFilterMode = True Then
MsgBox "Active worksheet have filters already in place"
Else
MsgBox "Active worksheet doesn't contain any filter
End If
End Sub
  • Run the code, click Run, or press the F5.

Check If Filter Is Applied or Not

  • A message box displaying the message ‘Active worksheet have filters already in place’.


Download Practice Workbook

We can download the practice workbook from here.

 


Related Articles

 

 

 

 

 

Get FREE Advanced Excel Exercises with Solutions!
Mukesh Dipto
Mukesh Dipto

Mukesh Dipto is an excellent marine engineer who loves working with Excel and diving into VBA programming. For him, programming is like a superhero tool that saves time when dealing with data, files, and the internet. His skills go beyond the basics, including Rhino3D, Maxsurf C++, AutoCAD, HTML, CSS, JavaScript, and WordPress. He got his B.Sc in Naval Architecture & Marine Engineering from BUET, and now he's switched gears, working as a content developer. In this role, he... Read Full Bio

4 Comments
  1. Very useful article, thanks a lot for sharing this Mukesh.

  2. Please help to me how do I deselect multiple criteria in the same field (more then 2) in Excel filter using VBA code.

    • Reply Avatar photo
      Musiha Mahfuza Mukta Jun 13, 2023 at 1:22 PM

      Thank you, Bibhuti Sutar, for your comment. Here, you can hide the cells which you want to deselect. So, only the wanted values will be visible. In this case, you can use the following VBA code. For example, I used the given dataset. Here, I want to deselect/remove the cities named New York, Dallas, and California.

      Sub show_defined_values()
        For i = 1 To 15
          If Range("B4:D15").Cells(i, 2).Value = "New York" Then
              Range("B4:D15").Cells(i, 2).EntireRow.Hidden = True
          ElseIf Range("B4:D15").Cells(i, 2).Value = "Dallas" Then
             Range("B4:D15").Cells(i, 2).EntireRow.Hidden = True
            ElseIf Range("B4:D15").Cells(i, 2).Value = "California" Then
             Range("B4:D15").Cells(i, 2).EntireRow.Hidden = True
          End If
      Next i
      End Sub


      You must edit this according to the range. If it doesn’t work, then please inform us with more details in the reply or you can send us your workbook in Exceldemy forum.
      Regards
      Musiha Mahfuza|Exceldemy

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo