VBA highlight filtered column (top row only)

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
409
I have located this code which highlights the whole column and changes all other columns to colour blank, is there a way of adjusting this so it only applies it to the top row (row 1) and not the whole column?

Any help would be much appreciated

Code:
[COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] Worksheet_Calculate() 
    ColorAutoFilter 
[COLOR=blue]End Sub[/COLOR] 

[COLOR=blue]Sub[/COLOR] ColorAutoFilter() 
    [COLOR=blue]Dim[/COLOR] FilterNum [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR] 
    [COLOR=blue]With[/COLOR] ActiveSheet 
        [COLOR=blue]If[/COLOR] .AutoFilterMode [COLOR=blue]Then[/COLOR] 
            [COLOR=blue]For[/COLOR] FilterNum = 1 [COLOR=blue]To[/COLOR] .AutoFilter.Filters.Count 
                [COLOR=blue]If[/COLOR] .AutoFilter.Filters(FilterNum).On [COLOR=blue]Then[/COLOR] 
                    .AutoFilter.Range.Columns(FilterNum).Interior.ColorIndex = 6 [COLOR=darkgreen]'yellow[/COLOR]
                [COLOR=blue]Else[/COLOR] 
                    .AutoFilter.Range.Columns(FilterNum).Interior.ColorIndex = xlNone 
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
            [COLOR=blue]Next[/COLOR] 
        [COLOR=blue]Else[/COLOR] 
            .Cells.Interior.ColorIndex = xlNone 
        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
    [COLOR=blue]End With[/COLOR] 
[COLOR=blue]End Sub[/COLOR]
 
Code:
      If .AutoFilter.Filters(FilterNum).On Then
        .AutoFilter.Range.Columns(FilterNum)[COLOR=#0000cd].Cells(1)[/COLOR].Interior.ColorIndex = 6  'yellow
      Else
        .AutoFilter.Range.Columns(FilterNum)[COLOR=#0000cd].Cells(1)[/COLOR].Interior.ColorIndex = xlNone
      End If
 
Upvote 0
Thanks Andrew, the result of this is no highlighting occurs what so ever.

This works for me:

Code:
Private Sub Worksheet_Calculate()
    ColorAutoFilter
End Sub
Sub ColorAutoFilter()
    Dim FilterNum As Long
    With ActiveSheet
        If .AutoFilterMode Then
            For FilterNum = 1 To .AutoFilter.Filters.Count
                If .AutoFilter.Filters(FilterNum).On Then
                    .AutoFilter.Range.Cells(1, FilterNum).Interior.ColorIndex = 6 'yellow
                Else
                    .AutoFilter.Range.Cells(1, FilterNum).Interior.ColorIndex = xlNone
                End If
            Next
        Else
            .Cells.Interior.ColorIndex = xlNone
        End If
    End With
End Sub
 
Upvote 0
Sorry both work, just my test file wasn't re-calculating correctly, my filer is on row 3 and this highlights that row not row 1? any ideas how to get this to go to row 1?
 
Upvote 0
That would be:

Rich (BB code):
Sub ColorAutoFilter()
    Dim FilterNum As Long
    With ActiveSheet
        If .AutoFilterMode Then
            For FilterNum = 1 To .AutoFilter.Filters.Count
                If .AutoFilter.Filters(FilterNum).On Then
                    .AutoFilter.Range.EntireColumn.Cells(1, FilterNum).Interior.ColorIndex = 6 'yellow
                Else
                    .AutoFilter.Range.EntireColumn.Cells(1, FilterNum).Interior.ColorIndex = xlNone
                End If
            Next
        Else
            .Cells.Interior.ColorIndex = xlNone
        End If
    End With
End Sub
 
Upvote 0
This code is working very well for me. Except I have one sheet that has some conditional formats set up and the highlight filtered column codes does not work. Is there a way to get around this aside from deleting my conditional formats? I should also say that if I manually run the macro it works fine but then I have to remember to run the macro each time I filter/unfilter the sheet. That seems to defeat the purpose of the macro.

Thanks for your help.
 
Upvote 0

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