Can you change colour of column header with VBA on Filter selection?

brendanolear

Active Member
Joined
Apr 26, 2008
Messages
366
I want users of a spreadsheet to easily identify columns they have already applied filters to. I know the tiny filter box changes; however some users :rolleyes: struggle to see where they have already applied filters and I am trying to make their lives a bit easier.

Any help appreciated.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Activate sheet with autofilter and execute this macro:
Code:
Sub ShowFilterInfo()

    Dim af As AutoFilter
    Dim f As Filter
    Dim sCols As String
    Dim c As Integer
    
    If Not ActiveSheet.AutoFilterMode Then
        MsgBox "AutoFilter does not exist in this sheet"
        Exit Sub
    End If


    Set af = ActiveSheet.AutoFilter
    For Each f In af.Filters
        c = c + 1
        If f.On Then
            sCols = sCols & c & ", "
        End If
    Next
    
    If Len(sCols) > 0 Then
        MsgBox "Filters are applied in the following columns: " & Chr(13) & Left$(sCols, Len(sCols) - 2)
    Else
        MsgBox "No filters applied found."
    End If


End Sub
 
Upvote 0
Well, the code I wrote gives a bit wrong information. First, it gives column as number and not as text. Second, the column index is relative to autofilter - not the beginning of sheet (i.e. A1 cell). If autofilter happens to begin at, say, F1, then the column index would not be the one expected. Here's correct code which gives correct column as text.
Code:
Sub ShowFilterInfo()


    Dim af As AutoFilter
    Dim f As Filter
    Dim sCols As String
    Dim c As Integer, iCol As Integer
    Dim firstCell As Integer
    Dim re As Object
    
    If Not ActiveSheet.AutoFilterMode Then
        MsgBox "AutoFilter does not exit in this sheet"
        Exit Sub
    End If


    Set af = ActiveSheet.AutoFilter
    firstCell = af.Range(1).Column
    Set re = CreateObject("VBScript.RegExp")
    re.Pattern = "[A-Z]+"
    
    For Each f In af.Filters
        c = c + 1
        If f.On Then
            iCol = c + firstCell - 1
            sCols = sCols & re.Execute(Cells(1, iCol).Address)(0) & ", "
        End If
    Next


    If Len(sCols) > 0 Then
        MsgBox "Filters are applied in the following columns: " & Chr(13) & Left$(sCols, Len(sCols) - 2)
    Else
        MsgBox "No filters applied found."
    End If
    
    Set re = Nothing


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,824
Messages
6,121,784
Members
449,049
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top