Conditional Formatting Based on Column by Which List Is Sorted

Zyniker

New Member
Joined
Feb 17, 2009
Messages
27
I have a worksheet that contains information in 5 Columns (B:F), I would like to conditionally format the header row to highlight the cell by which the data is currently being filtered (e.g., if the data is filtered by the header row of ColumnB, I would like the header cell in ColumnB to be conditionally formatted, let's go with red background just to make this simple).

Further, I would like to create a cell (doesn't really matter where it is on the worksheet) that displays the text in the header cell of the column by which the data in the worksheet is currently being sorted (in other words: if the data is currently being sorted by ColumnB and the header cell in ColumnB says "foo" I want the cell in question to display the text "foo").

Thanks in advance for any help!
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi Zyniker,

This should get you started. Formula in B1 should be copied across to F1. Place the macro in a standard module. All credit for this macro is credited to Stephen Bullen as noted in the macro itself.

Before Filter

Excel Workbook
BCDEF
1 
2DayMonthYearCodeCost
3MonJan2007a1
4TueFeb2008foo2
5WedMar2009c3
6ThuApr2010d4
data


After Filter

Excel Workbook
BCDEF
1 =foo
2DayMonthYearCodeCost
4TueFeb2008foo2
data


Code:
Function FilterCriteria(Rng As Range) As String
    'By Stephen Bullen
    Dim Filter As String
    Filter = ""
    On Error GoTo Finish
    With Rng.Parent.AutoFilter
        If Intersect(Rng, .Range) Is Nothing Then GoTo Finish
        With .Filters(Rng.Column - .Range.Column + 1)
            If Not .On Then GoTo Finish
            Filter = .Criteria1
            Select Case .Operator
                Case xlAnd
                    Filter = Filter & " AND " & .Criteria2
                Case xlOr
                    Filter = Filter & " OR " & .Criteria2
            End Select
        End With
    End With
    
Finish:
    FilterCriteria = Filter
End Function
 
Upvote 0
Wow .... nice trick. :)
That's very useful when printing several copies of the same sheet using different columns for the sorting!
 
Upvote 0
It worked perfectly. Thanks. :)

For future reference, would it be relatively simple to change the code to perform virtually the same function...but altered in order to 'highlight' by what criteria the data is being sorted instead of filtered?
 
Upvote 0

Forum statistics

Threads
1,223,715
Messages
6,174,065
Members
452,542
Latest member
Bricklin

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