|
Filter Data in Excel to Show Rows/Data That Meet Multiple Criteria for One Field - AutoFilter
This free Excel macro filters data in Excel based on multiple criteria for one field in the data set. This macro uses the autofilter feature and specifically filters data based on two separate criteria. This allows you to narrow down a data set based on more than one parameter and to better display specific sets of data within Excel.
The two criteria are listed as "Criteria1" and "Criteria2" and in order to change what is filtered, just replace the "Enter Criteria Here" text with your own text, characters, or numbers.
How Filter Macros Work
All of the elements below often appear within autofilter macros.
Range
This should be the start of the data set, table, or list which you would like to filter. This can also be the entire range reference to the table. For example, if the table was from cell A1:D450, you could put that as the range or you could put A1 as the range. You can do this because the autofilter feature in Excel will automatically try to determine the total range which you would like to filter.
Field
This is the number of the column within the data table that you would like to filter. The first field (1) is the very first column in the data set that will be filtered. This means that if your data starts in column B and your range is Range("B1") and you want to filter based on column D, you would put 3 in for the field.
Criteria
This is the criteria by which you would like to filter. Some of the macros have symbols within the quotation marks after this argument and those symbols (such as <,>,*,?, etc.) should be left where they are in order to retain the functionality of the macro.
Operator
You will not have to change this. This is simply the way to perform different types of filter features in Excel.
Where to install the macro: Module
Excel Macro to Filter Data in Excel to Show Rows/Data That Meet Multiple Criteria for One Field - AutoFilter
Sub AutoFilter_in_Excel_AND_Operator_One_Field()
Range("A1").AutoFilter Field:=1, Criteria1:="Enter Criteria Here", Operator:=xlAnd, Criteria2:="Enter Criteria Here"
End Sub
How to Install the Macro
- Select and copy the text from within the grey box above.
- Open the Microsoft Excel file in which you would like the Macro to function.
- Press "Alt + F11" - This will open the Visual Basic Editor - Works for all Excel Versions.
Or For other ways to get there, Click Here.
For Excel Versions Prior to Excel 2007 Go to Tools > Macros > Visual Basic Editor
For Excel 2007 Go to Office Button > Excel Options > Popular > Click Show Developer tab in the Ribbon. Then go to the Developer tab on the ribbon menu and on the far left Click Visual Basic
- On the new window that opens up, go to the left side where the vertical pane is located. Locate your Excel file; it will be called VBAProject (YOUR FILE'S NAME HERE) and click this.
- If the Macro goes in a Module, Click Here, otherwise continue to Step 8.
- Go to the menu at the top of the window and click Insert > Module
- Another window should have opened within the Visual Basic Editor's window. Within this new window, paste the macro code. Make sure to paste the code underneath the last line of anything else that is in the window.
- Go to Step 8.
- If the Macro goes in the Workbook or ThisWorkbook, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Then, at the bottom of the list that appears, double-click the ThisWorkbook text.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Go to Step 8.
- If the Macro goes in the Worksheet Code, Click Here, otherwise continue to Step 8.
- Directly underneath your excel file called VBAProject(your file's name here), click the Microsoft Excel Objects folder icon to open that drop-down list.
- Within the list that appears you will see every worksheet that is in that excel file. They will be listed as such: Sheet1(NAME OF SHEET HERE) and under that will be Sheet2(NAME OF SHEET HERE). Select the sheet in which you want the macro to run and double-click that sheet.
- A new window inside the Visual Basic Editor's window will appear. In this new window, paste the code for the macro. Make sure to paste this code underneath the last line of any other code which is already in the window.
- Repeat steps b and c for every sheet you want the macro to work in. Putting the macro in one sheet will not enable it for any other sheets in the workbook.
- Go to Step 8.
- Close the Microsoft Visual Basic Editor window and save the Excel file. When you close the Visual Basic Editor window, the regular Excel window will not close.
- You are now ready to run the macro.
Similar Helpful Excel Resources
I have a large spreadsheet containing several hundred rows of data. I want
to filter the list and I want to be able to show the number of records that
match the filter criteria. For example, un-filtered the total number of
records in the list is displayed and, when filtered, the number of records in
the list that match the criteria is displayed.
Can onyone help me please?
Kind regards,
Cliff
This topic on Ozgrid provides just what I need, but limited to only 2 criteria - my VBA is not up to extending the code to the ten criteria that I need. Can anyone point me in the right direction please?
VB:
Function AutoFilter_Criteria(Header As Range) As String Dim strCri1 As String, strCri2 As String Application.Volatile With Header.Parent.AutoFilter With .Filters(Header.Column - .Range.Column + 1) If Not .On Then Exit Function strCri1 = .Criteria1 If .Operator = xlAnd Then strCri2 = " AND " & .Criteria2 ElseIf .Operator = xlOr Then strCri2 = " OR " & .Criteria2 End If End With End With AutoFilter_Criteria = UCase(Header) & ": " & strCri1 & strCri2 End Function
If you like these VB formatting tags please consider sponsoring me in support of injured Royal Marines
I have the code which filters my spreadsheet and copies that dataset but I think I need to add an IF that skips the copy line when the criteria is not met. Any ideas?
Thanks all!!
Please see attached sample for this description to make sense...
I'm trying to create a formula (used in sheet 2 column B) that would generate the results in sheet2.B based on the contents of sheet 1. sheet2.B2 would contain a formula searching for the both "Y" in sheet1.C and the text in sheet2.A2 (in this case "E&P"). When each match is found, the contents of sheet1.A should be returned to sheet2.B, as shown in the mockup.
There will be multiple matches (at least 15-20) for each search criteria, and I don't know how many there will be ahead of time.
I've tried various formulas, and they either have incorrect syntax and return every row in a range, or only return the first match correct match each time instead of all correct matches...
I found some UDFs posted online that claim to do this, but I've gotten them into the spreadsheet and they generate #NUM errors. I would prefer to work with Excel-native formulas if possible, because I'm going to eventually hand this off to someone else to maintain and they may not have the level of expertise to deal with UDFs.
Thanks!!!
Dear all,
I have a table with 4 columns. From the following table, I want to see the rows that have:
1. year 2010
2. color red
3. value over 100
-------A--------B-------C-------D
name-------year------color----value
1-chris-----2010-----red------120
2-bob-------2011-----blue----140
3-nik--------2010-----red-----140
4-aron------2010-----red-------30
I want that excel shows me those rows on an other part of the spreadsheet, including the corresponding names from column A.
This means, I would like to see the following result on another part of my spreadsheet:
chris---2010---red---120
nik----2010----red---140
any idea how this could work??
Hello,
Have a table of sales people,
Want to set rewards program for top sales people based on n# of sales that exceed $X.
so for the source range that feeds the pivot table, I have a vba decision derived decision column title "Huge Sale" that is applied to each sale (does this sale exceed $x, if true then put "yes" in decision column, else "no").
Now, what I am trying to figure out, on my pivot table, I want it only to show salespeople who have had at least n# of "Huge Sales". Is there a way I can set a filter on my pivot table to show only row groups where Count of Huge Sales exceeds n?
If I can do that, user inputs what a huge sale is ($ threshold value) and how many sales before reward (# threshold). And the table would just spit out the sales people to reward.
Any ideas?
Current Row labels a
Huge Sale (show "yes" only)
Salesperson
Data:
Count of huge sale
Hi,
I have been working with personal record data in multiple workbooks, using sumif, index, vlookup etc to show various required info.
What I now need to do is take all the records of people aged between 16-18 and list this in a new worksheet.
I can get all the records I need in a pivot table but it needs to be something I can add columns to in order to gather further info.
Any help would be much appreciated.
Hi,
How would I show all records in a list that meet a given criteria (in this example = "A") and display them on a seperate worksheet?
Thanks
Hi Guys,
Code:
Selection.AutoFilter Field:=9, Criteria1:="Clear"
In the above code, Field 9 refers to the 9th column. In my table each column does have a header name. Column 9's header is "KIND". From time to time this KIND column may move around in the table. Is there a way to filter based on the column header name and not the columns placement?
Code:
Selection.AutoFilter Field:="KIND", Criteria1:="Clear"
I know the above does not work, but is there something similar that's available? Or maybe a way to dynamically index the columns?
|
|