Filter on Value, Font or Fill color

In Excel 2016 and higher there are new commands on the Cell menu that make it easy to filter a table based on the active cell's value, font color or fill color. This article discusses how you can access these features with a macro.

The Cell menu is the menu that pops up when you right click a cell.

FilterMenu

Note
: There are two ways that a cell's font or fill color can be set. One is by the Fill and Font controls for example in the Font group on the Home tab. The other is by Conditional Formatting in the Styles group on the Home tab. The great thing about the new color filtering features is that it works with colors set either way.

When you select one of the filter options on the Cell menu Excel will guess what your filter range is if you've selected only a single cell. If you have any empty rows in your table Excel may not use the range you intend.

How do I access these features with a macro

In the basic example below I show how to use one of the built-in options in your VBA code using the Commandbars Execute method. Why not use the Range AutoFilter method instead? The problem with the AutoFilter method is that it requires you to specify the font or fill color of the active cell. That's easy to do when the colors are set by the Font group controls, but when the colors are set because of Conditional Formatting it is impractical, if not near impossible, to do.

Why? Because Excel does not give us a direct way to tell in code what font or fill colors a cell is displaying as a result of conditional formatting. Our code would have to work through the conditional formatting rules and figure out the one in effect, if any, and then figure out the formatting applied, if any. It is much easy to use the Execute method and have Excel do all this work.

The code example will create a new worksheet or workbook with every record with the same Fill interior Color/Pattern or Shading style of the active cell.

You can change the number in this part of the macro if you want to filter on the font color or value.

'Call the built-in filter option to filter on ACell
Application.CommandBars("Cell").FindControl _
(ID:=12233, Recursive:=True).ExecuteControl

12232 = Filter by Selected Cell's Value
12233 = Filter by Selected Cell's Color
12234 = Filter by Selected Cell's Font Color
12235 = Filter by Selected Cell's Icon

Why do I use the Control Id instead of the control caption ?
If you use the ID the code will work in every language version of Excel

Tip: See also the tips part below the macro on this page.
Maybe there is something useful there for you if you want to change the code.

VBA code example

In my example code I filter on the Fill interior color/Pattern or Shading style in a normal range or Table.

Select a cell with a Fill interior color/Pattern or Shading style and Run the macro. The macro will create a new worksheet for you with the filter results. Every time you run the code it will delete the worksheet first so you are sure that the worksheet have the latest filter results.

Read this part in the macro good "Optional set the Filter range". If there are empty rows or columns in your data in a normal range you can make sure that Excel uses the correct data range in this code part. The Macro below is also working in Win Excel.


Tip : Create a new workbook instead of a worksheet
If you want to create a new Workbook with the filter results instead of a Worksheet then :

Delete this part

'Delete the sheet MyFilterResult if it exists
On Error Resume Next
Application.DisplayAlerts = False
Sheets("MyFilterResult").Delete
Application.DisplayAlerts = True
On Error GoTo 0

And replace this line

Set WSNew = Worksheets.Add

With

Set WSNew = Workbooks.Add.Worksheets(1)
05/09/2024
Web design by Will Woodgate