Show AutoFilter Criteria in Status Bar

October 17, 2011 by datapig Leave a reply »

One of my 12 fans (Lester) emailed to remind me that I have a blog. I almost forgot. So with a good kick in the pants, I'm back to blogging.

.
While I was away, Microsoft awarded me with the coveted Microsoft Excel MVP award for the fourth time. So I guess I better start earning that distinction here. Here we go.

.

Today, I'll show you a nifty trick that allows you to actually see the AutoFilter criteria you have applied on an Excel table.

.

This is actually a pretty common request. When you've got an AutoFilter applied, it would often be nice to see how the table is actually AutoFiltered. Alas, Excel's only accommodation is to give you a generic count of records on the status bar.

.

.

I've come up with a piece of code that will give you more details in the status bar. Like this:

.

.

Here is how you can implement this:

Step 1:

Copy and paste this 'ShowAutoFilterCriteria' Macro into a standard module.

Visual Basic:
  1. Sub ShowAutoFilterCriteria()
  2. Dim AF As AutoFilter
  3. Dim TargetFilter As Filter
  4. Dim TargetField As String
  5. Dim strOutput As String
  6. Dim i As Integer
  7.  
  8. 'Check if the sheet is not auto-filtered, then exit
  9.     If ActiveSheet.AutoFilterMode = False Then
  10.         Application.StatusBar = False
  11.         Exit Sub
  12.     End If
  13.    
  14.     Set AF = ActiveSheet.AutoFilter
  15.    
  16. 'Loop through the Filters of the AutoFilter
  17.     For i = 1 To AF.Filters.Count
  18.         TargetField = AF.Range.Cells(1, i).Value
  19.        
  20.         'Get the Filter object. If it is on, then get the standard filter criteria
  21.         Set TargetFilter = AF.Filters(i)
  22.         If TargetFilter.On Then
  23.          
  24.          'Avoid error when more than two filters are applied
  25.          On Error GoTo CriteriaOverflow
  26.          strOutput = strOutput & "    " & TargetField & TargetFilter.Criteria1
  27.              
  28.              'Account for special filters
  29.              Select Case TargetFilter.Operator
  30.                 Case xlAnd
  31.                    strOutput = strOutput & " And " & TargetField & TargetFilter.Criteria2
  32.                 Case xlOr
  33.                    strOutput = strOutput & " Or " & TargetField & TargetFilter.Criteria2
  34.                 Case xlBottom10Items
  35.                    strOutput = strOutput & " (bottom 10 items)"
  36.                 Case xlBottom10Percent
  37.                    strOutput = strOutput & " (bottom 10%)"
  38.                 Case xlTop10Items
  39.                    strOutput = strOutput & " (top 10 items)"
  40.                 Case xlTop10Percent
  41.                    strOutput = strOutput & " (top 10%)"
  42.              End Select
  43.       End If
  44. SkipItem:
  45.     Next
  46.    
  47. 'Display the filters if there are any
  48.    If strOutput = "" Then
  49.    Application.StatusBar = False
  50.    Else
  51.    Application.StatusBar = strOutput
  52.    End If
  53. Exit Sub
  54.  
  55.  
  56. 'Error handler when more than two filters are applied
  57. CriteriaOverflow:
  58.     strOutput = strOutput & "    " & TargetField & "= Multiple Filters"
  59. ErrorHandler:         Resume Next
  60.  
  61. End Sub

.

Step 2:

In the Workbook's SheetCalculate event, enter a call to the 'ShowAutoFilterCriteria' macro.

.

Step 3:

Be sure you enter a volatile formula in any dummy cell on the sheet. This will trigger the Workbook_SheetCalculate event. For example, I use the Now() function. This ensures that when I change the Autofilter, the macro will fire automatically.

.

If all went well, you'll have a jazzy way to see the criteria you've applied to your AutoFilters.

.

Here's a sample file if you want to see it in action.

Advertisement

14 Responses

  1. Lynda says:

    Sweet!

    p.s. - Thanks, Lester! (lol!)

  2. Subbaraman says:

    Congratulations and best wishes on the MVPS title recredit/renewal.

  3. Lester says:

    Dang -- I asked for an Access tip!

    LOL -- Just kidding. Thank you, Mike.

  4. Ulrik says:

    Cool and quite useful - thanks for sharing!

  5. Graham says:

    The text in the status bar persists when the spreadsheet is closed or another opened. You need to clear it using the Workbook Deactivate or Close event.

  6. Dave in Oregon says:

    I implemented this in a workbook that needed it, and noticed an interesting side effect. In my case it was beneficial, but it might not be for some.

    You use the NOW() function on the worksheet in question, along with that worksheet's Calculate event. Turns out other autofilters on other worksheets ALSO trigger the NOW() function on the original worksheet, and thus the Calculate event. So setting this up on one worksheet in effect sets it up on EVERY worksheet!

  7. Tom says:

    Cool trick. Is there anyway to show the search criteria in addition to the number of results? I like the idea of seeing the search criteria, but I reference the number of records quite a bit, so wouldn't want to give it up.

  8. Adalerto says:

    In the line above NEXT command you use a tag SKIPITEM: that is not used for a macro.
    In the line:
    'Avoid error when more than two filters are applied
    On Error GoTo CriteriaOverflow
    On the line where you have put "CriteriaOverflow" wouldn't "SkipItem" ?

  9. James says:

    Very clever tip ... !!!

    Extremely handy when dealing with large data sets ...

  10. Colin says:

    Could this be setup to work with:
    1. the Sub ShowAutoFilterCriteria() is stored only in a module of my Personal.xlsb file
    2. the Call method added to the Workbook_SheetCalculate event of the ThisWorkbook object of the target workbook, and
    3. =Now() included in a cell on the target worksheet
    so that I don't have to add the Sub to each target workbook?

  11. Chris says:

    Colin, I had the same question as you regarding putting this in my personal macro workbook.

    You can do this in the Workbook / SheetCalculate call.

    Application.Run "personal.xls!ShowAutoFilterCriteria"

    In other words "FILENAME-where-module-is-stored!MACRO-NAME"

    thanks DP for the tip, I am sharing this with some co-workers as well.
    -Chris

  12. Lynda says:

    I tweaked it a little to have it called from the Workbook_SheetSelectionChange event, 'cause my engineers got too confused when it showed a filter that was on a different sheet... (sigh...)

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

* Copy this password:

* Type or paste password here:

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>