How to AutoFilter a Table based on a Cell Value in Excel

,

This post will guide you how to auto filter a given range of cells based on a certain cell value in Excel 2013/2016. How do I use VBA Autofilter function to filter the data based on a cell value in Excel.

For example, you wish to quickly filter the data in range A1:B5 based on a cell value that you entered in A9, and then the data will be filtered automatically in range A1:B5. Of course, you can use the inbuilt filter functionality with copy-paste operations, and it will take you a lot of time to do it manually. So you can use VBA AutoFilter to filter data quickly so that to save your time.

AutoFilter a Table based on a Cell Value using VBA Macro


Just do the following steps:

Step1: Go to your worksheet that you want to create VBA AutoFilter macro to autofilter the data based on cell value that you typed.

autofilter based on cell value1

Step2: right click on the sheet tab in your current worksheet, and select View Code from the pop-up menu list, and then the Visual Basic for Application window will appear.

autofilter based on cell value2

Step3: you need to copy and paste the below VBA Macro code into the code window. And then close it.

Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.Address = Range("A9").Address Then
       Range("A1:B5").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("A8:A9")
   End If
End Sub

autofilter based on cell value3

Step4: type the first value “version” in Cell A8, and then type another value “2016” as a criteria, press Enter key.

Step5: you would see that your data is filtered based on cell value that you entered.

autofilter based on cell value4

autofilter based on cell value5

Note: the range A1:B5 is your data that you want to filter out. And A8:A9 is your criteria range cells that will be filtered based on.

When you change the cell value in Cell A9, your data also be filtered based on cell value in A9 automatically.