Written by Allen Wyatt (last updated August 21, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Kris knows how to filter information in a worksheet by color. The filtering tools allows him to select which color of text or background he wants to display. Kris wonders if there is a way to filter so that multiple colors are displayed. If he is filtering by cell contents, he can easily pick multiple matching items to be displayed but cannot figure out how to do it for colors.
There are two ways you can go about this. The first approach doesn't rely on filtering at all. Instead, sort your data by color. If you do two (or more) sorting passes, you should be able to get the desired colors all next to each other. Then, select the remaining rows (the one using colors you don't want to see) and hide those rows.
Again, this is just a workaround, and it can work fine for relatively short lists of data. The second approach is to use a helper column. All you need to do is to enter, in each cell of the helper column, the name of the color in that row—i.e., red, orange, yellow, green, etc. You can then filter by that column and thereby display multiple colors in your filtered results.
If your data table is too long, typing all those colors could get monotonous and be prone to errors. You could, if desired, create two very short user-defined functions that would simply return the color of a specific cell. This UDF returns the color of the text itself:
Function FColor(cell) FColor = cell.Font.ColorIndex End Function
The following is a variation that returns the interior color of the cell itself:
Function IColor(cell) IColor = cell.Interior.ColorIndex End Function
In your helper column, enter a formula that relies on the desired UDF. For instance, if you want to filter by the interior color in column A, you would enter this formula in the helper column of row 1:
=IColor(A1)
The cell should now contain the color number. Copy this cell down as far as necessary, and then filter by the contents of this column. Since you can select multiple numeric values to appear in your filtered data, you effectively end up with multiple colors in the results.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13611) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
When you are working with large amounts of data in a worksheet, filtering that data can make the process much simpler. ...
Discover MoreWhen you filter data in a worksheet, Excel also allows you to apply sorting orders to that data. Here is a ...
Discover MoreNeed to remove filters and display all rows and columns in all your worksheets? It is not easy to do manually, but with a ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-01-27 08:59:20
Jomili
Years ago I made this macro to filter for all fill colors at one go. It can easily be adapted to filter by font color:
Sub ColorFilterToggler()
Dim rng As Range
Set rng = ActiveSheet.UsedRange
If rng.SpecialCells(xlCellTypeVisible).Rows.Count = rng.Rows.Count Then
'No rows hidden
FilterAllColors
Else
'Some rows hidden
rng.EntireRow.Hidden = False
End If
End Sub
Sub FilterAllColors()
Dim lrow As Long
Dim lcol As Long
Dim lLastRow As Long
Dim rShow As Excel.Range
Application.ScreenUpdating = False
ActiveSheet.UsedRange
lcol = ActiveCell.Column
lLastRow = Cells(Rows.Count, lcol).End(xlUp).row
For lrow = 2 To lLastRow
If Cells(lrow, lcol).Interior.Color <> 16777215 Then
If rShow Is Nothing Then
Set rShow = Cells(lrow, lcol)
Else
Set rShow = Union(rShow, Cells(lrow, lcol))
End If
End If
Next
Range(Cells(2, lcol), Cells(lLastRow, lcol)).EntireRow.Hidden = True
If Not rShow Is Nothing Then rShow.EntireRow.Hidden = False
Application.ScreenUpdating = True
End Sub
2019-01-22 10:34:01
Gary
Nice UDFs! I don't have a lot of experience with UDFs, but it doesn't seem like I've had to use the file name of my personal workbook (where I save all of my code), in the past, when I used a UDF. For some reason this one requires that I add Personal.xlsm! when I type the UDF in order for it to work. What's wrong?
Thank you!
Gary
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments