Column Formatting Based On a Filter

Written by Allen Wyatt (last updated February 22, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and 2021


6

In a large table of data, Ed would like to be able to quickly scan and see if a particular column is being actively filtered. He wonders if there is any way to apply conditional formatting to change the background color of a column when there is a filter in play that is based on that column.

There are a few ways you can approach this task. All of them involve macros, and the purpose of each macro is to determine if a filter is in play for a particular column. One option is to create a function that examines the worksheet for a filter and, if it finds one in place, checking each column in the filtered area to see if there is a filter in play in that column. The following macro does just that.

Sub ColorFilterColumn()
    Dim flt As Filter
    Dim iCol As Integer
    Dim lRow As Long
    Dim rTemp As Range
    Dim bFullCol As Boolean

    ' Set as True if you want entire column shaded
    bFullCol = False

    If ActiveSheet.AutoFilterMode Then
        iCol = ActiveSheet.AutoFilter.Range.Column
        lRow = ActiveSheet.AutoFilter.Range.Row
        Application.EnableEvents = False
        For Each flt In ActiveSheet.AutoFilter.Filters
            If bFullCol Then
                Set rTemp = Cells(lRow, iCol).EntireColumn
            Else
                Set rTemp = Cells(lRow, iCol)
            End If

            If flt.On Then
                rTemp.Interior.Color = vbYellow
            Else
                rTemp.Interior.ColorIndex = xlColorIndexNone
            End If

            Set rTemp = Nothing
            iCol = iCol + 1
        Next flt
        Application.EnableEvents = True
    End If
End Sub

If the macro locates a filter that is at work, it either highlights (in yellow) the first cell in the filtered table or the entire column that has the filter. The determination as to whether a cell or the entire column is highlighted is based on the True/False value assigned to the bFullCol variable.

If you prefer, you could create a function that returns True or False based upon whether a filter is in effect for a particular column. With such a function you could create a conditional formatting rule that formats the column based upon the value returned.

Function bHasFilter(rcell As Range) As Boolean
    Dim lBaseCol As Long
    Dim lCol As Long

    Application.Volatile
    bHasFilter = False

    If ActiveSheet.AutoFilterMode Then
        With ActiveSheet.AutoFilter
            lBaseCol = .Range.Column
            lCol = rcell.Column - lBaseCol + 1
            If lCol > 0 And lCol <= .Filters.Count Then
                If .Filters(lCol).On Then bHasFilter = True
            End If
        End With
    End If
End Function

To use this function, simply use a formula such as the following in your worksheet or in the conditional formatting rule:

=bHasFilter(F23)

The function first checks to see if there is a filter in effect. If so, then it calculates whether the column of the cell passed to the formula is within the range of filtered columns. (The row referenced in the formula doesn't really matter.) If so, then it checks to see if the filter is turned on for that column.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13410) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Changing How Arrows Look

If you use Excel's graphic capabilities to insert a line or an arrow into a worksheet, you can change how that arrow ...

Discover More

Errors when Copying References to External Cells

If you copy a cell that contains a reference to external data, do you get an error? It could be due to the complexity of ...

Discover More

Problem with Missing Context Menu Option

When you right-click a cell, does it seem that the Context menu is missing an item or two? Here's how to get those items ...

Discover More

Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!

More ExcelTips (ribbon)

Entering a Date in a Filter

The filtering capabilities of Excel can come in handy for zeroing in on the data you want to work with. When your data ...

Discover More

Filtering is Unavailable when Selecting Entire Worksheet

Filtering is easy to use in Excel, but sometimes you might have problems activating the filtering capabilities of the ...

Discover More

Non-PivotTable Slicers and Timelines

When working with a PivotTable, slicers and timelines can make short work of large data sets. This tip looks at all the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is seven more than 3?

2022-08-23 16:16:59

J. Woolley

My Excel Toolbox includes the following dynamic array function to list a worksheet's auto filters (Data > Filter):
=ListAutoFilters([Target],[SkipHeader])
For details, see my comment here: https://excelribbon.tips.net/T012839_Using_a_Filtered_Value_in_a_Formula.html


2018-09-18 11:19:22

Alessandro

I've tried the bHasFilter formula but i was unable to use it directly in conditional formatting, however i've solved using function in hidden cells and then using cond formatting referring to that cells.
Also it seems the function didn't works if the argument range is in a different sheet. (always return FALSE)
In my opinion the macro is not really useful, but the function is somewhat good


2015-11-16 06:03:36

DaveS

Having a helper row containing the (volatile) UDF returning TRUE/FALSE provides the automatic response to changes in the filter, so there's no reliance on manually firing a macro.

The approaches described in the tip might not work in every case (for example, on Tables; and conditional formatting is not without its quirks). But I've used something similar without problems - when there are a lot of columns, having a colour change can make it easier to spot the filtered column when scrolling across. Don't forget, folks, that someone asked if this could be done; just because it is of no use to you doesn't mean it's 'useless' to someone else.


2015-11-15 08:08:41

Willy Vanhaelen

I agree with Bob. This tip is rather useless because Excel already idicates the filtering columns involved by changing the drop-down arrow in the column header button into a filter symbol when a filter is applied. And that is reliable !!!


2015-11-15 07:22:34

Mike H

This works when the data being filtered has not been "Format as table". When I tried this with an Excel Table it didn't seem to work as AutoFilter macro command was not specific to the Table especially if the AutoFilter was not on at the startr Please could you test with a Table and see if you have the same issue. I believe it will need to have code which includes ListObjects to get this to work.


2015-11-14 15:41:08

Bob Beechey

I created macros like these but discarded them as they did not seem useful. I wanted something either (1)in a UDF that could be part of conditional formatting but this proved unreliable. (2) there was no event that was triggered by changing the filter.
The result is therefore relying on a macro that has to be fired manually and does not update of its own accord and so the colouring does not necessarily reflect the filtering.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.