Selectively Hide AutoFilter Dropdowns

May 20, 2010 by datapig Leave a reply »

During one of my nerdy adventures, I came across the need to selectively hide AutoFilter Dropdowns. That is to say, apply an AutoFilter to a table, but only let the user see and interact with one or two of the dropdowns. Here's the deal:

 

The Problem:

I started with this table, which the client wanted to filter by Month and Primary Campaign.

 

 

Applying a standard AutoFilter will show dropdowns on all columns - even those between the ones I need.
We didn't want the end-users of this report to filter by sales or the visual arrow indicator.

 

 

So I tried to apply AutoFilters with only the columns I need selected. No love from Excel.

 

So Excel won't let me selectively apply AutoFilters, and it looks as though there is no way to hide the dropdowns via the user interface.

It didn't look good.

 

 

The Simple Fix:

After a little research, I found this property in the Object model (VisibleDropDown). That is, the AutoFilter object has a poperty called VisibleDropDown. Setting this to False will hide the dropdown.

For example, this little diddy will hide the second dropdown in the given AutoFilter:

Range("A2:E2").AutoFilter Field:=2, VisibleDropDown:=False

 

In my case, I want to only show the dropdowns for columns 1 and 5. So here is what I used:

Sub HideAutoFilterDropdowns()
With Range("A2:E2")
.AutoFilter Field:=1, VisibleDropDown:=True
.AutoFilter Field:=2, VisibleDropDown:=False
.AutoFilter Field:=3, VisibleDropDown:=False
.AutoFilter Field:=4, VisibleDropDown:=False
.AutoFilter Field:=5, VisibleDropDown:=True
End With
End Sub

 

Voilà!  Everything works as it normally would with a standard AutoFilter setup - only, I've hidden a few of the dropdowns.

 

 

The Unnecessarily Complex Fix:

Finally, what kind of nerd would I be if I didn't provide an unnecessarily complex alternative that allows you to loop through the columns.

Actually, this piece of code would come in handy if you wanted to selective hide dropdowns across many many columns.

Visual Basic:
  1. Sub HideAutoFilter()
  2. Dim i As Integer
  3. Dim MyRange As Range
  4. Dim ValidFilters As Variant
  5.  
  6. Set MyRange = Range("A2:Z2") '<<ValidFilters = Array(1, 5, 7, 15, 20, 23) '<<
  7.  
  8. 'Start with hidden dropdowns
  9. For i = 1 To MyRange.Columns.Count
  10. MyRange.AutoFilter Field:=i, VisibleDropDown:=False
  11. Next i
  12.  
  13. 'Now unhide only the one in your ValidFilters array.
  14. For i = 0 To UBound(ValidFilters)
  15. MyRange.AutoFilter Field:=ValidFilters(i), VisibleDropDown:=True
  16. Next i
  17. End Sub

Advertisement

9 Responses

  1. AlexJ says:

    How's the performance of this technique accross many columns?

  2. datapig says:

    Alexj: Lightning fast. The same performance you get by simply applying the AutoFilter.

  3. Jon Peltier says:

    Couple things. First, make sure if a column is not listed that you make the dropdown visible.

    Second and more important, in your example if i=2, instr() is >0 because 20 is in the list. Thus 2 will not be hidden, even though 2 itself is not in the list. Don't be lazy, turn ValidFilters into an array using Split, then loop through the elements to check for matches.

  4. Rick Rothstein (MVP - Excel) says:

    @Jon,

    You can still use InStr instead of Splitting the text and then looping it; just change ValidFilters to this...

    ValidFilters = "*1*5*7*15*20*23*" 'asterisk delimited list

    and then use this If..Then statement to do the test...

    If InStr(ValidFilters, "*" & i & "*") = 0 Then

  5. datapig says:

    Damn you Peltier....you got me. I adjusted the code to use an array.

    Rick: Great idea - just too late for me to save face.

  6. Tim buckingham says:

    Setting an array of auth app.username allows you to also leave yourself a back door with the following:

    If application.uername dev then
    HideAutoFilter
    End If

    that way only cust will have restricted filters.

  7. Khushnood Viccaji says:

    I wanted to check the possibility of showing the filter arrows with a bit of flexibility.
    The code below maybe a bit crude, but it does the job.
    A couple of changes were made in the worksheet:
    1. The Header Row is not Row2, but Row4
    2. I have provided for TRUE or FALSE to be entered in Row2

    Based on the value in Row2 for each column, the AutoFilter arrows will be Visible or Hidden.
    The user can edit these values to show / hide the drop-down arrows, and re-run the macro.

    Visual Basic:
    1. Sub HideAutoFilterDropdowns_v2()
    2.       Dim c, iFieldCounter As Integer, iCountFields As Integer
    3.  
    4.       ' determine count of cells in header row
    5.   10  iCountFields = Range("rg.HeaderRow").Cells.Count
    6.       ' set iFieldCounter to 1
    7.   20  iFieldCounter = 1
    8.  
    9.       ' select first cell in header row
    10.   30  Range("rg.HeaderRow").Select
    11.   40  ActiveCell.Select
    12.  
    13.   50  For Each c In Range("rg.HeaderRow")
    14.           ' apply VisibleDropDown property as per cell in Row2
    15.   60      Range("rg.HeaderRow").AutoFilter _
    16.                   Field:=iFieldCounter, VisibleDropDown:=ActiveCell.Offset(-2, 0)
    17.           ' increment iFieldCounter
    18.   70      iFieldCounter = iFieldCounter + 1
    19.           ' select next cell in header row
    20.   80      ActiveCell.Offset(0, 1).Select
    21.   90  Next c
    22.  
    23.       ' go back to first cell in header row
    24.   100 Range("rg.HeaderRow").Select
    25.   110 ActiveCell.Select
    26.   End Sub

    Khushnood

  8. Lynda says:

    I wanted to see if you could still filter those columns using ALT+Arrowdown and found something surprising - you can't filter those columns but it turns what was the filtering list into a data validation list for that cell! So, lock those cells so they can't be selected, maybe?

  9. Mark says:

    Wonderful - just what I needed. Thanks, M

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>