Written by Allen Wyatt (last updated January 11, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021
Roger is wondering if there is way to use the COUNTIF function using the cell background color as the "if" criteria. He has a calendar and he wants to be able to count the number of days he highlights in purple or other colors.
The short answer is that COUNTIF cannot be used to check for background color or any formatting; it can only test for values. If you only need to figure out the number of purple cells once or twice, you can use Excel's Find and Replace feature to figure it out. Follow these steps:
Figure 1. The Find tab of the Find and Replace dialog box.
Figure 2. The Find Format dialog box.
Of course, these steps might get tedious if you want to count more than a color or two. Or, you may want the count so you can use it in a different calculation of some type. In these instances, you would do better to create a user-defined function that examines the cells and returns a count. One such macro is CountColorIf:
Function CountColorIf(rSample As Range, rArea As Range) As Long Dim rAreaCell As Range Dim lMatchColor As Long Dim lCounter As Long lMatchColor = rSample.Interior.Color For Each rAreaCell In rArea If rAreaCell.Interior.Color = lMatchColor Then lCounter = lCounter + 1 End If Next rAreaCell CountColorIf = lCounter End Function
In order to use the macro, all you need to do is provide a cell that has the background color you want tested and the range to be tested. For instance, let's say that cell A57 is formatted with the same purple background color you use in your calendar cells. If the calendar is located in cells A1:G6, then you could use the following to get the count of purple cells:
=CountColorIf(A57, A1:G6)
It should be noted that if you change the color in a cell in your calendar, then you'll need to do something to force a recalculation of the worksheet. It seems that Excel doesn't do an automatic recalculation after changing background color.
There are, of course, many different ways you could approach the problem and develop user-defined functions such as CountColorIf. Here are a couple other websites that contain information that may be helpful in this regard:
http://www.cpearson.com/excel/colors.aspx https://www.ozgrid.com/VBA/sum-count-cells-by-color.htm
There are also some third-party add-ons available that you could use. One such add-on suggested by readers is Kutools for Excel. You can find more information on the add-on here:
https://www.extendoffice.com/product/kutools-for-excel.html
One final note—the ideas in this tip work fine if you are working with cells that are explicitly filled with colors. They will not work with cells that are colored using Conditional Formatting. That is an entirely different kettle to boil, as Conditional Formatting doesn't really give you anything you can latch onto easily.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11725) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
The SUMIF function is supposed to work just fine doing comparisons with text values. This isn't a hard-and-fast rule, ...
Discover MoreWhen working with data taken from the real world, you often have to determine which certain conditions were met, such as ...
Discover MoreIf you need to insert into a cell the name of the workbook in which a worksheet is contained, you can use the CELL ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2025-01-12 12:06:03
J. Woolley
My Excel Toolbox includes the following function to return the RGB fill color of a cell as a decimal value:
=FillColor([Cell])
The default value of Cell is the formula's cell. If Cell is a multi-cell range, only its first cell applies. This function is essentially the same as the ClColor function described in Tomek's earliest comment below; it ignores conditional formatting.
My Excel Toolbox also includes the following function to convert a decimal RGB color value into a 6-character hexadecimal text value:
=ColorAsHex(ColorRGB)
For example, the decimal RGB value for red is 255; its hexadecimal text value is FF0000.
To return the RGB fill color of a cell as hexadecimal text, use this formula:
=ColorAsHex(FillColor([Cell]))
See https://sites.google.com/view/MyExcelToolbox/
For more about colors, see my earliest comment (2024-07-05) here: https://excelribbon.tips.net/T009092_Showing_RGB_Colors_in_a_Cell.html
2025-01-11 19:08:26
Note to the picture in my previous comment:
The Figure 1 shows 5 violet cells found, but if you count the colors you will only find 4. That is because the cell A12 has conditional formatting applied to it with yellow background and it takes precedence when displayed. The explicit background color is still 15086320 as indicated in B12.
I just forgot remove conditional formatting, but it proves the point that this approach does not necessarily reflect what you see.
I actually suspect that Roger would like to count the cells by color that is defined by conditional formatting. Contrary to what Allen said, this information can be obtained by VBA by using the following syntax:
<cell>.DisplayFormat.Interior.Color
It will get the color of what is displayed, whether it is explicit, or overridden by conditional formatting.
Unluckily this syntax cannot be used in user defined functions, but can be used in subs.
I have some ideas how to overcome this limitation, so if someone is interested please reply here or send me an e-mail.
2025-01-11 17:10:58
Tomek
A different approach would be to create an user defined function that would return a color of the cell background, e.g,:
'===============================================
Public Function ClColor(cl As Range)
ClColor = cl.Interior.Color
End Function
'==============================================
Then you can create a mirror range of cells containing the color values for the range of cells in the calendar, using a formula =ClColor(address) (see Figure 1 below) .
Let's assume this range is named "CalendarShades"
Then in a cell where you want the count enter:
=COUNTIFS(CalendarShades,15086320))
where the number 15086320 corresponds to violet color of the cell background in the example spreadsheet.
Note that the range of cells does not have to be contiguous or rectangular.
I suggest creating the mirror range on a second sheet, simply by creating the duplicate sheet, then replacing the content of the relevant cells with the formula for the color from original sheet.
Like it was stated in Allen's tip, this approach will also work only with cells that are explicitly filled with colors.
Figure 1.
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 © 2025 Sharon Parq Associates, Inc.
Comments