Using COUNTIF with Colors

by Allen Wyatt
(last updated October 27, 2018)

6

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:

  1. Select the cells that make up your calendar.
  2. Press Ctrl+F. Excel displays the Find tab of the Find and Replace dialog box.
  3. Click the Options button, if it is available. Excel expands the dialog box. (See Figure 1.)
  4. Figure 1. The Find tab of the Find and Replace dialog box.

  5. Make sure the Find What box is empty.
  6. Click the Format button. Excel displays the Find Format dialog box. (See Figure 2.)
  7. Figure 2. The Find Format dialog box.

  8. Click the Choose Format From Cell button, at the bottom of the dialog box. The Find Format dialog box disappears and the mouse pointer changes to a plus sign with an eyedropper next to it.
  9. Click on a cell that is formatted like those you want to find. (In other words, click on a purple cell.) The mouse pointer returns to normal.
  10. Click Find All. The Find and Replace dialog box expands to list all cells matching the format, and there is a count of the cells at the bottom of the dialog box.
  11. Click Close to dismiss the Find and Replace 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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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 Office 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Saving WordArt Graphics as Files

WordArt can be a handy tool for creating all sorts of flourishes on traditional text. If you want to save the graphic ...

Discover More

Quickly Dumping Array Contents

Variable arrays are used quite often in macros. If you use an array once in your macro and then need to reuse it for ...

Discover More

Getting Rid of the Lock Screen

The Lock Screen is handy on mobile devices, but may be a bother on your desktop PC. Here's how to turn the Lock Screen ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Converting Radians to Degrees

When applying trigonometry to the values in a worksheet, you may need to convert radians to degrees. This is done by ...

Discover More

Using the COLUMN Function

Need to know the column number for use in a formula? The worksheet function you want is the COLUMN function, described in ...

Discover More

Counting the Number of Blank Cells

If you need to count the number of blank cells in a range, the function to use is COUNTBLANK. This tip discusses the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 9?

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.


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.