Conditional Format Columns Only When Filter Applied

mnyankee

New Member
Joined
Mar 20, 2023
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I need help figuring out how to conditionally format a column or columns based on another column being filtered or not. In other words if the Match column is filtered for either true or false, then I need formatting applied to the Value 1 and Value 2 columns and then have that formatting removed when the Match column is not being filtered. Is this possible? Thanks in advance for any help you can provide.

Value 1Value 2Match
11TRUE
23FALSE
30FALSE
42FALSE
55TRUE
65FALSE
74FALSE
88TRUE
91FALSE
1010TRUE
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Is this possible?
Yes. There's probably more elegant ways of doing this than what I propose, but give this a try on a copy of your workbook until something better comes along. Firstly, you need to be able to trigger an event when the filter changes. The way I usually do this is with a Worksheet_Calculate() event - coupled with a CountA() formula in a spare cell on your sheet. I've used cell D1 looking at column A (you can hide column D if you like). So the basic sheet looks like this:
mnyankee.xlsm
ABCD
1Value 1Value 2Match11
211TRUE
323FALSE
430FALSE
542FALSE
655TRUE
765FALSE
874FALSE
988TRUE
1091FALSE
111010TRUE
Sheet1
Cell Formulas
RangeFormula
D1D1=COUNTA(A:A)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$A$1:$C$11D1


Next, you place a Worksheet_Calculate() event code in the sheet module of the sheet in question. If you're unfamiliar with how to do this, right click on the sheet tab name, select View Code and copy/paste the code below into the window that appears on the right.
VBA Code:
Private Sub Worksheet_Calculate()
    Application.EnableEvents = False
    Dim r As Range, c As Range, LRow As Long, i As Long, s As String
    LRow = Cells(Rows.Count, "C").End(xlUp).Row
    Set r = Range("A2:C" & LRow)
    For Each c In Range("C1:C" & LRow)
        If c.EntireRow.Hidden = False Then i = i + 1
    Next c
    With r
        If i = LRow Then
            .Interior.Color = xlNone
            .Font.ColorIndex = 0
            .Font.Bold = False
        Else
            With .Columns("A:B")
                For Each c In Range("C2:C" & LRow)
                    If c.EntireRow.Hidden = False Then
                        s = UCase(c.Value)
                        Exit For
                    End If
                Next c
                If s = "TRUE" Then
                    .Interior.Color = vbGreen
                    .Font.Bold = True
                Else
                    .Font.Color = vbRed
                    .Font.Bold = True
                End If
            End With
        End If
    End With
    Application.EnableEvents = True
End Sub

You haven't indicated what formats you want depending on whether True or False is selected, so for demonstration purposes I've made the cell interior green for true, and the font red for false. When you filter for True, you'll get this:
mnyankee.xlsm
ABCD
1Value 1Value 2Match11
211TRUE
655TRUE
988TRUE
111010TRUE
Sheet1
Cell Formulas
RangeFormula
D1D1=COUNTA(A:A)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$A$1:$C$11D1


When you filter for false, you'll get this:
mnyankee.xlsm
ABCD
1Value 1Value 2Match11
323FALSE
430FALSE
542FALSE
765FALSE
874FALSE
1091FALSE
Sheet1
Cell Formulas
RangeFormula
D1D1=COUNTA(A:A)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$A$1:$C$11D1


The formatting is removed when no filter is set. Obviously, you can change the formats to whatever you want. Here's a link to the demo file: mnyankee.xlsm
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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