Can excel Filter formula filter by font colour?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
does anyone know if you can filter by font colour using the new filter formula ?
if so please give example of filtering by RGB 255,0,0

if not any ideas of how i can do this in vba?

thanks
Tony
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
This will go through a Range (I used Sheet1, Columns 1 (or A)) and tell you which cells have red font as Debug.Print in Immediate Window.
VBA Code:
Sub clrFilter()
Dim myRng As Range, cell As Range
Set myRng = Sheet1.Range(Cells(1, 1), Cells(Sheet1.UsedRange.Rows.Count, 1))
For Each cell In myRng
    If cell.Font.Color = 255 Then
        Debug.Print "Cell " & cell.Address & " has red font."
    End If
Next cell
End Sub
 
Upvote 0
This will go through a Range (I used Sheet1, Columns 1 (or A)) and tell you which cells have red font as Debug.Print in Immediate Window.
VBA Code:
Sub clrFilter()
Dim myRng As Range, cell As Range
Set myRng = Sheet1.Range(Cells(1, 1), Cells(Sheet1.UsedRange.Rows.Count, 1))
For Each cell In myRng
    If cell.Font.Color = 255 Then
        Debug.Print "Cell " & cell.Address & " has red font."
    End If
Next cell
End Sub
Code is placed in Sheet1.
 
Upvote 0
The Filter function cannot see colour, so you need VBA.
To get the code you can turn on the macro recorder & apply an autofilter to filter the colour & you have your code.
 
Upvote 0
I have thrown together a quick code that checks the current cell text color, changes it, and then converts back.

You could possibly use something like this to loop through existing cells and do things if the RGB values match.

(also, Excel uses a default 'long' formation instead of RGB)

VBA Code:
Sub TestColor()
    
    Dim OriginalColor As Long
    OriginalColor = GetFontColor(Selection)
    
  
    Dim RGB1 As String
    RGB1 = RGB1 & ConvertLongToRGB(Selection.Font.Color, "R")
    RGB1 = RGB1 & ", " & ConvertLongToRGB(Selection.Font.Color, "G")
    RGB1 = RGB1 & ", " & ConvertLongToRGB(Selection.Font.Color, "B")
    MsgBox (RGB1 & " Is the original color of selected cell - Long value of: " & OriginalColor)
    

    Dim NewFontColor As Long
    NewFontColor = ConvertRGBToLong(255, 0, 0)
    SetFontColor Selection, NewFontColor
    
    Dim RGB As String
    RGB = RGB & ConvertLongToRGB(Selection.Font.Color, "R")
    RGB = RGB & ", " & ConvertLongToRGB(Selection.Font.Color, "G")
    RGB = RGB & ", " & ConvertLongToRGB(Selection.Font.Color, "B")
    
    MsgBox (RGB & " Is the new color of selected cell - Long value of: " & NewFontColor)
    
    MsgBox ("Now we will revert back to the way it was.")
    SetFontColor Selection, OriginalColor
    
    

    
End Sub

Function SetFontColor(ByVal Target As Range, Col As Long) As Long
    
    Target.Font.Color = Col
    
End Function


Function GetFontColor(ByVal Target As Range) As Long
    GetFontColor = Target.Font.Color
End Function




Public Function ConvertRGBToLong(iRed As Integer, iGreen As Integer, iBlue As Integer) As Long
    ConvertRGBToLong = RGB(iRed, iGreen, iBlue)
End Function

Public Function ConvertLongToRGB(lColor As Long, sRGB As String) As Integer
    Select Case UCase(sRGB)
        Case "R"
            ConvertLongToRGB = lColor Mod 256
        Case "G"
            ConvertLongToRGB = lColor \ 2 ^ 8 Mod 256
        Case "B"
            ConvertLongToRGB = lColor \ 2 ^ 16 Mod 256
    End Select
End Function

This is the page I used for reference when converting colors back and forth.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,031
Members
448,940
Latest member
mdusw

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