VBA filter column macro helps filter the data in a specific Column. Let us see different example macros to filter the records or rows based on items in one column. Example Data to Explain the VBA Filter:
Here is the Example data to illustrate the macro on VBA Filter Column. This Example Data sheet contains 100 records with sample data items. We have created employee records with columns: Serial Number, Name, Country, Department, DOJ and Salary to explain this topic.
VBA to Filter data in a specific Column:
Let us see an Example VBA macro to filter the records in a worksheet. This Example shows how to filter Data based on an item in Column C using Excel VBA.
We have 100 rows in the data sheet and Country details in Column C. Let us filter the data to show only records from county US. i.e; We need to apply the filter on Column C and set the criteria as US.
Here is the VBA Code created with Macro Recorder:
Sub Macro1() ' Macro1 Macro Columns("A:F").Select Selection.AutoFilter ActiveSheet.Range("$A$1:$F$101").AutoFilter Field:=3, Criteria1:="US" End Sub
Let us Clean the code to have the final VBA code to filter the data in Column.
Sub sbAT_VBAMacroToFilterColumn() ' VBA Code to filter records of Columns A to F based on the data item in Column C ActiveSheet.Range("$A$1:$F$101").AutoFilter Field:=3, Criteria1:="US" End Sub
We have removed all unnecessary code and left with the required code for filtering the data.
Let us see the code in-depth:
We are using the AutoFilter method of the range. In the above code, we have created a macro to apply the filter on Range A1:F101 of the active worksheet. And applied the filter on Column 3 and set the criteria as US.
This Example macro will filter the records in Range A1:F101 and show all records with US in Column 3 , i.e; in Column C. Here is the final out put of the VBA Filter Column Macro:
VBA Filter Column – Download the Example File
Here is the Example file with sample data. You can download the Excel VBA Macro file and Explore to see the VBA code to filter column data.
Executing the Macro: VBA Filter Column:
- Open the Example File
- Go to Data sheet
- Open VBA Editor (Press Alt+F11 to open it)
- And Run the Macro by pressing F5 Key.
Now you can see the filtered records in Active sheet.
Thanks for the VBA CODE provided for filtering the data based on an item in a specified Column. It is really helpful.
Thanks a lot.
Hi ,
This is really helpful. Thanks for the code.
However, I am facing a challenge of filtering a column based on each value in that column; some of them may not be known.
For example, in one of the columns, I have names and want to apply filter on it. However, I know few names that can be applied in filters. But there can be couple of more names which I am not aware. Hence I need to apply filters for each available value in that column. Is there a way to do it?
How to get cell value after applying the filter
HI,
ActiveSheet.Range(“$A$1:$F$101″).AutoFilter Field:=3, Criteria1:=”US”
In the above line, the range is known. Please let me know how to make it dynamic with unknown range.
Thank you!!
Bobby
maybe this helps when the range is unknown:
Sub Names()
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets(“sheet1”)
sh.UsedRange.Select
With Selection
.AutoFilter
End With
End Sub