Excel VBA to Filter in Same Column by Multiple Criteria (6 Examples)

In this tutorial, we will demonstrate the use of Excel VBA to filter in the same column by multiple criteria. In Microsoft Excel using VBA to filter a dataset is a fast and convenient way. To illustrate the concept of this article, we will apply filters in the same column based on multiple criteria using the VBA filter.


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

In this article, we will explain 6 examples of using Excel VBA to filter in the same column by multiple criteria. For all the examples in this article, we will use the same dataset given below. The dataset contains Date. name of the City, and Sales for each city on a particular day.

6 Examples of Excel VBA to Filter in Same Column by Multiple Criteria


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

First and foremost we will remove specific values from the same column using the Excel VBA filter by multiple criteria. For instance, we will filter the City column of our given dataset. We will remove the values California & Texas from the column City with an Excel VBA filter. Let’s see the steps to perform this action.

STEPS:

  • To begin with, right-click on the worksheet tab named REMOVE.
  • In addition, 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. Another way to open that code window is to press Alt + F11.
  • Furthermore, 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
  • After that, 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

  • Finally, we get results like the image below. We can see that the values California & Texas have been filtered from the column City.

Read More: Filter Multiple Criteria in Excel with VBA (Both AND and OR Types)


2. Keep Particular Values in the Same Column with Excel VBA

Unlike the previous example in this example, we will keep a particular value in the same column instead of removing it. To do this we will apply an Excel VBA filter by multiple criteria in the same column. In the previous example, we have removed the value ‘California’ & ‘Texas’. But, in this example, we will keep these values after applying filters whereas we will remove the other ones. Let’s see the steps to follow for this example.

STEPS:

  • First, right-click on the worksheet tab named KEEP.
  • Next, 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. We can also press Alt + F11 to get that code window.
  • Then, 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
  • After that, to run the code click on the Run or press the F5 Key.

Keep Particular Values in Same Column with Excel VBA

  • As a result, we can see the result in the below image. Only the values present in the City column are California and Texas. Other values have been filtered.


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

In the third example, we will apply Excel VBA to filter by multiple criteria with advanced criteria ranges in the same column. We will use the dataset of our previous examples. From the image below we can see the advanced criteria also. We are going to filter the data set for two criteria. The first one is that the city will be New York and the sales amount will be greater than $3000. The second one is that the city will be in California and the sales amount will be less than $3000. Just follow the below steps to perform this method.

STEPS:

  • Firstly, right-click on the worksheet tab name ‘Criteria_range’.
  • Secondly, click on the option ‘View Code’.

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

  • So, we will get a new blank VBA code window for the active worksheet.
  • Thirdly, input the following code in that code window:
Sub Advanced_Criteria()
Range("B4:D15").AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=Range("F6:G8")
End Sub
  • Then, 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

  • Lastly, we get results like the image below. The filtered data in the image below follows our criteria range.


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

Another example of applying filters in the same column is to use VBA with OR criteria in Excel.

We will use two criteria in our dataset. The OR criteria will return a value if the values from our dataset meet any of the defined two criteria. To perform this example in our dataset follow the below steps.

STEPS:

  • In the beginning, right-click on the active sheet tab named OR.
  • Next, 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. We can also press Alt + 11 to open that code window.
  • Then, 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
  • Now, to run the code click on the Run or press the F5.

Filter in Same Column Using VBA with OR Criteria in Excel

  • Lastly, we get our dataset filtered for the defined criteria range.

Read More: How to Filter Based on Cell Value Using Excel VBA


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

The AND criteria will return the values which will meet both criteria that we will fix. Using AND criteria we can filter in the same column using Excel VBA by multiple criteria. We will filter the data in our dataset for sales values between $2000 & $3500. Let’s take a look at the steps to apply AND criteria with the VBA filter in our dataset.

STEPS:

  • To begin with, right-click on the active sheet named AND.
  • In addition, click on the option ‘View Code’.

Insert AND Criteria with Excel VBA to Filter in Same Column

  • So, a new blank VBA code window will open for the active worksheet. An alternative to open that code window is to press Alt + 11.
  • Next, 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
  • Furthermore, click on the Run or hit the F5 key to run the code.

Insert AND Criteria with Excel VBA to Filter in Same Column

  • Lastly, we can see the result for filtered data in the image below.


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

In the last example, we will apply VBA to filter with data range. To do this we will fix multiple criteria in the same column. Despite the use of AND criteria, we are showing this example since we will filter the dataset for a date range. Follow the below steps to apply a VBA filter with the data range in our dataset.

STEPS:

  • First, go to the active worksheet tab named DateRange and right-click.
  • Next, 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. We can also hit Alt + 11 to get that code window.
  • Then, 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
  • Now, to run the code click on the Run or press the F5 key.

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

  • In the end, we get the Date column filtered by our given criteria.

Read More: Excel VBA: How to Filter with Multiple Criteria in Array


Excel VBA to Turn off AutoFilter

In the following image, we can see that the filter is already applied to the dataset. In this section, we will use VBA to turn off the filter applied in this dataset.

Excel VBA to Turn off AutoFilter

So, let’s see the steps to perform this method.

STEPS:

  • Firstly, right-click on the active worksheet.
  • Secondly, select the option ‘View Code’.
  • So, a blank VBA code window will appear for that worksheet.
  • Thirdly, copy the following code in that code window:
Sub Turn_Off_Filter()
Worksheets("TurnOff").AutoFilterMode = False
End Sub
  • After that, click on the Run or press the F5 key to run the code.

Excel VBA to Turn off AutoFilter

  • Lastly, we can see the result in the following image. Our dataset no longer has any filters.


Turn on AutoFilter in Excel Using VBA

In this section, we will use a VBA code to turn on the auto filter for our dataset. Follow the below steps to do this.

STEPS:

  • To begin with, select the active worksheet tab named ‘TurnOn’ and right-click on it.
  • In addition, select the option ‘View Code’.

Turn on AutoFilter in Excel Using VBA

  • So, it will return a blank VBA code window.
  • Furthermore, 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
  • Next, press the F5 key or click on the Run to run the code.

Turn on AutoFilter in Excel Using VBA

  • As a result, we can see filter icons in the header cells of our dataset.

Read More: How to Remove Filter in Excel VBA


Check If the Filter Is Applied or Not

Suppose we are working with thousands of cells. It’s not possible to identify if the dataset has filters applied to it only by looking at it. So, in this method, we will use a VBA code to see if the dataset contains a filter or not.

Check If Filter Is Applied or Not

Let’s see the steps which we have to follow in this method.

STEPS:

  • In the beginning, right-click on the active worksheet tab.
  • Next, select the option ‘View Code’.
  • Then, a blank VBA code window will open for that worksheet. We can also open that code window by pressing Alt + F11.
  • After that 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
  • Now, to run the code click on the Run or press the F5.

Check If Filter Is Applied or Not

  • Lastly, we get a message box displaying the message ‘Active worksheet have filters already in place’.


Download Practice Workbook

We can download the practice workbook from here.


Conclusion

In conclusion, this tutorial explains different examples of using the Excel VBA filter with multiple criteria in the same column. So, to put your skills to the test, download the sample worksheet included in this article. Please leave a comment in the box below if you have any questions. Our team will try to reply to your message as quickly as possible. In the future, keep an eye out for more innovative Microsoft Excel solutions.


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