Written by Allen Wyatt (last updated July 4, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
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 few 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 http://xldynamic.com/source/xld.ColourCounter.html
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, 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!
Excel includes a surprising number of functions you can use to round your data. Two such functions are FLOOR and CEILING, ...
Discover MoreIf you need to do some work in the base-8 numbering system (octal), you'll love two worksheet functions provided by Excel ...
Discover MoreWhen you need to count a number of cells based upon a single criteria, the standard function to use is COUNTIF. This tip ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-04-22 18:18:42
David Czuba
A better solution is to not use CountIF at all, but to filter your data by color. Place the function =SUBTOTAL(103,[range]) below the last number in the column, then filter by color. The 103 in the function refers to COUNTA (count non-empty cells) and excludes row data hidden by the filter. If the data is in a Table, it’s easier. In Table Design tab, check the box for Total row, then use the Count in the last row of the column, then filter the column data by color.
2019-06-27 22:23:56
Brett Phipps
If it's conditional formatting you can just use the same rules the conditional format used to begin with.
2019-05-06 18:31:04
Tonja
I'm new at the more intermediate functions of excel - so please bare with me. Here's my question.
Can you determine the value of a cell on one tab based on the text of a cell on a different tab.
i.e.
On my overall inventory tab
if I type say a "JaxOffice" in cell C3, I want it to subtract or add the value in cell D3 or E3 or F3 from cell D3 or E3 or F3 on another tab. Is there a way to make excel recognize text this way? Colors would be too confusing I think, as there could be a number of options when moving inventory around.
Does this make sense? We store inventory in 5 places. If I have to ship, I have to subtract from tab a and add to tab b or whatever...I'm trying to find if there's a way to do this in one step. Thanks for any help - even if it's "no, that's not possible".
2019-03-19 04:42:47
Kevin Johnson
Would "cell.DisplayFormat.Interior.ColorIndex" give access to the colour set by conditional formatting? The following illustrates filling empty cells having a specific colour set by a conditional format.
Sub cmdUnallocated()
'
' Set StaffPlan available slots to Unallocated (Unalloc.)
'
Dim rngStaffPlan As Range
Dim cellSlot As Range
'
Set rngStaffPlan = Range("StaffPlan")
For Each cellSlot In rngStaffPlan
If cellSlot.DisplayFormat.Interior.ColorIndex = 35 Then cellSlot.Value = "Unalloc."
Next cellSlot
End Sub
2018-11-05 20:42:02
Norm
Maybe you folks can help. I have a range of cells with the letter "D" or "N" in them. Some have a font color of black and some are blue. With slight modification of the function above (see below) i can get it to count the total number cells with blue or black letters. What I am trying to do is to be able to count not just the total but the number of blue "N", black "N", blue "D", black "D" as well. Is there a way to change this code to do that?
Function CountColour(rng As Range, clr As Range)
Application.Volatile
Dim c As Range
For Each c In rng
If c.Font.Color = clr.Font.Color Then
CountColour = CountColour + 1
End If
Next
End Function
2018-10-29 12:42:52
David Robinson
I should think you could replicate some conditional formats by expressing the condition as criteria within the COUNTIF formula itself, and this means you won't need to do the Find All method described above. For a colour scale you'll need to see how the colour thresholds are set and attempt to replicate them in code.
But yes, it does seem tricky, you'll have to resolve each conditional format one at a time.
2018-10-27 21:44:42
Eric
"That is an entirely different kettle to boil, as Conditional Formatting doesn't really give you anything you can latch onto easily."
So does that mean it can't be done?
This is something that I've needed for a long time.
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