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.


Download Practice Workbook

We can download the practice workbook from here.


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

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 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 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 value 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.


2. Keep Particular Values in 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 Same Column Using VBA with OR Criteria in Excel

Another example to apply 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.


5. Insert AND Criteria with Excel VBA to Filter in 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 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. In spite of 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.


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.


Check If 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’.


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.

Mukesh Dipto

Mukesh Dipto

Hi there! I am Mukesh Dipto. I graduated with a bachelor's degree in engineering. Currently, I am working as a technical content writer in ExcelDemy. You will find all my articles on Microsoft Excel on this site. Outside of the workplace, my hobbies and interests include sports and meeting new people. I also enjoy sports. My favorite sports are football (to watch and play) and Badminton (play).

We will be happy to hear your thoughts

Leave a reply

ExcelDemy
Logo