|
Display the AutoFilter Criteria Applied to a Data Set in a Cell in Excel - UDF
This free Excel UDF (user defined function) allows you display the filter criteria that has been applied to a data set in Excel. This means that it will output, in text, the actual criteria used to filter the data. This allows you to quickly and easily know what criteria you are using to filter your data set.
This is a great function to use if you often filter data, especially if you then disseminate the filtered results to others who might want to know what you used to filter the data. This is also easy to use since you only have to input the cell reference of the header to the filtered data in order to return the criteria for the filter. It is probably best to put this function in a cell directly above the filter header so that it is easy to locate and understand.
Where to install the macro: Module
UDF to Display the AutoFilter Criteria Applied to a Data Set in a Cell in Excel
Function AUTOFILTERSHOWCRITERIA(Header As Range) As String
Dim strCri1 As String
Dim 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
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
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
Ok here's my problem:
I have a column of Store Names ie. Wal-Mart, Target, etc.
When I apply the AutoFilter to display all Wal-Marts for example, I want a cell, we'll call it Cell:B4 on a different worksheet within the same workbook to display the name of the store selected in the AutoFilter (Wal-Mart in this example). I need that same cell to change anytime the AutoFilter is changed to another store.
So, Cell:B4 should always display what the filter is set to ie. All; Wal-Mart; Target; or whatever store I choose.
Can anyone help?
Thanks in advance
Hi folks!
I've read the following help thread http://www.ozgrid.com/VBA/autofilter-criteria.htm and it works fine. But it won't work if there are more than 2 filter criteria selected. I suppose, I would have to change it from "xlOR" to "xlFilterValues" and do something with "array". But I don't know how.
Can anyone help?
Thanks!
Nidi
Hello!
I'm very new to all the VBA and Macro's.
I have a datasheet (sheet2) with filters, and an overview page (sheet1), where all totals are displayed.
I need to have the the filtercriteria as well (Sheet1), and used the OZGRID page""Display Excel AutoFilter Criteria" to get the code.
I just can't seem to get more then 2 filtercriteria to show. More return with "#Value!"
What do I have to put in the code (and where) to make it show more then 2 filtercriteria?
I've included the workbook, so you can see waht I've done and need.
Micha
Hello Board
I have a line of code in my macro that is supposed to autofilter a specific column, by selecting >= a certain cell, and
Hello,
I am trying to get data from a range of cells to display in a single cell, based on a criteria. I have a list of job codes, with the names of who worked the job. I want the output to be a table with the job codes on it, that lists each name with a comma separator. I have attached a workbook to give an example.
The raw data will always change, so I cannot use a formula that references specific cells.
I have a password protected sheet, the data is autofiltered, I wanted a simple macro to un-filter all the data but levae the AutoFilter buttons intact.
Tried making a macro that memorized my steps:
-unprotect
-remove autofilter
-add autofilter
-reprotect
some reason not working
Is there a simple piece of VBA or something I can add to just have the data become unfiltered but the AutoFilter buttons still there?
Never mind - I got it, courtesy code posted here by Dave Peterson.
Hi there,
I'm looking for some code that would allow me to fill a listbox on a userform with the resulting columdata (column D) after the columns to the left have been autofiltered.
Anyone who could be of assistance, please ?
Thanks in advance
Eric
Hey guys!
I need a macro which checks, if there is a certain autofilter criteria setup for a certain column on a different sheet.
Sometimes this sheet has a autofilter applied for the column, sometimes not. I can't simply do something like this (as you propably know):
Code:
If Sheet62.Range("C14:C65000").AutoFilter Field:=1, Criteria1:="Y" Then ...
In fact I wanna check several columns, if there are AutoFilters applied and then warn the user.
Thanks for your help!
|
|