Using COUNTIF with Colors

by Allen Wyatt
(last updated June 11, 2018)

17

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
http://www.ozgrid.com/VBA/sum-count-cells-by-color.htm
http://xldynamic.com/source/xld.ColourCounter.html
https://www.techrepublic.com/blog/windows-and-office/conditional-formatting-tricks-sum-values-in-excel-by-cell-color/

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

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, and 2016.

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

Printing a Font List

Getting a list of fonts available in a document is not something you can easily do in Word. That is, unless you put the ...

Discover More

Listing All Styles in the Style Drop-Down List

Want to see all the built-in styles in the Style drop-down list? Here's the quick way to do it.

Discover More

Switching Headers in a Frozen Row

Excel allows you to "freeze" rows in your worksheet. What if you want the rows that are frozen to change as you scroll ...

Discover More

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!

More ExcelTips (ribbon)

Using the IRR Function

When working with finances, you often need to know the rate of return on a given investment. The most common type of ...

Discover More

Using the EOMONTH Function

If you need to determine the date of the last day in a month, it's hard to beat the flexibility of the EOMONTH function. ...

Discover More

Strange ATAN Results

You may use Excel's trigonometric functions to do some quick calculations, and suddenly notice that the results in your ...

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 - 3?

2018-06-21 21:21:15

Peter Mestrum

Hi

How can i get the function to update with each new calculation. The function works perfectly but if a new coloured area is added or the calculations change the function does not update


2018-06-02 03:38:11

Eleven Lever

I tried this method, and it works pefrectly when the cells manually filled with color.
I cannot use this with Automatically filled cells ( Conditonal Formatting).. any method for that?


2018-02-02 10:31:32

Spencer

There is an easier way using the =SUBTOTAL(103,StartRange:EndRange) function. If you, for example, have a list of names, highlighted in different colours, at the bottom of that range insert a function as above. The, use DATE -> Filter to filter your list. On the click-down for the filter you will see an option for "Filter by colour". Filter by a specific colour, and the =SUBTOTAL(103) function will count the resulting names.


2017-11-28 02:38:11

margaret

I am counting the number of colored cells in each row of a range of cells. Is there a way to fill the formula down while keeping the reference cell constant? i.e., I chose cell E1 as my reference for the cell color; I have put my formula with the CountColorIf below that in column E; I am looking at the range F:AP in rows 2 through 72.

The formula works well in each row, but if I fill down, it bumps up the color reference cell address to the next row, so I have to edit the formula in each row to refer to to the right color.

My workaround is to insert a whole column and format with that color, rather than just having one reference cell. I put an x in each cell so I don't get "formula refers to an empty cell" errors. I can then fill the formula down in the CountColorIf column E without having to edit.

Perhaps you have a better way?


2017-11-28 01:30:50

margaret

I am counting the number of colored cells in each row of a range of cells. Is there a way to fill the formula down while keeping the reference cell constant? i.e., I chose cell E1 as my reference for the cell color; I have put my formula with the CountColorIf below that in column E; I am looking at the range F:AP in rows 2 through 72. The formula works well in each row, but if I fill down, it bumps up the color reference cell address to the next row, so I have to edit the formula in each row to refer to to the right color. I suppose if I make a whole column and format with that color, it would work in each row properly without editing it.


2017-11-02 07:52:21

Ron

Any suggestions for doing summation with cells not in order?

As in =CountColorIf(A57, A1:G6) but instead of A1:G6, I want to look at A1,B6,D19 and some those if the color criteria is met. When I put in the range as a group of cells, it errors.

Thanks!


2017-02-07 11:46:42

Chris

Thank so much! This formula does not work for me when using conditionally formated values, any idea how to solve?


2016-08-11 18:14:14

Carl

Hi,

Using Excel 2013. I want to return the count of the number of cells that are colored a specific color AND have a specific value.
FOR EXAMPLE: I have APPLES (red), GRAPES (green) and STRAWBERRIES (red). I want to only return the number of red apples, not everything red. Can I do this? Please help
P.S. I am not using COUNTIF to count apples only as I am using that formula to return that value in a different cell.


2016-03-09 01:09:37

Allan

I think I've fixed it and now have it returning the correct answers. The code is:

Function CountColorIf(rSample As Range, rArea As Range) As Long
Dim rAreaCell As Range
Dim lMatchColor As Long
Dim lCounter As Long
Dim StartDate As Date
Dim EndDate As Date
Dim ContactDate As Date
StartDate = CDate(Sheets("Report Page").Range("B1").Value)
EndDate = CDate(Sheets("Report Page").Range("B2").Value)
lMatchColor = rSample.Interior.Color
For Each rAreaCell In rArea
ContactDate = CDate(rAreaCell.offset(0, 15).Value)
If rAreaCell.Interior.Color = lMatchColor And _
ContactDate >= StartDate And _
ContactDate <= EndDate Then
lCounter = lCounter + 1
End If
Next rAreaCell
CountColorIf = lCounter
End Function


2016-03-08 12:16:21

Allan

This function has worked well for me and I have tried to alter it to include some date comparisons. Unfortunately it now returns a #VALUE! error and will not pass the counter value to the function. It does count the coloured cells in the date range correctly but skips over the last couple of lines when testing the dates. The referenced date cells are all formatted as dates.
Any suggestions?

My code is Function CountColorIf(rSample As Range, rArea As Range) As Long
Dim rAreaCell As Range
Dim lMatchColor As Long
Dim lCounter As Long
Dim StartDate As Date
Dim EndDate As Date
Dim ContactDate As Date


StartDate = DateValue(Sheets("Report Page").Range("B1").Value)
EndDate = DateValue(Sheets("Report Page").Range("B2").Value)

lMatchColor = rSample.Interior.Color
For Each rAreaCell In rArea
ContactDate = DateValue(rAreaCell.offset(0, 15).Value)
If rAreaCell.Interior.Color = lMatchColor And _
ContactDate >= StartDate And _
ContactDate <= EndDate Then

lCounter = lCounter + 1
CountColorIf = lCounter
End If
Next rAreaCell

CountColorIf = lCounter
End Function


2016-01-30 05:33:17

Aemon

I have a query
i want to count characters from a column with highlighted cells in excel
eg
1 S
2 S
3 I
4 S
5 R
6 S
7 S
8 S
9 S
10 S
11 S
12 S
13 R
14 R
15 S
where cell 2,3,4,5,6,7,10,13,14,15 are highlighted cells

output should be
number of S in highlighted area
S=6
number of R in highlighted area
R=3
number of I in highlighted area
I=1


2015-09-11 20:04:00

Kevin C

What a clear and concise description of how to do something.

Congratulations!!


2015-09-01 09:19:27

ken charlton

Hi I have tried this function , but I get an error
I have countcolourIF(V76,R3:R89)As Long
my error is " compile error . Expected list separator or )" and highlights the colon ":" Im new to this any Ideas please
ken


2015-05-22 21:25:08

Peter

As in =COUNTIF(A2:A100,">15")


2015-05-22 21:21:13

Peter

@Kevin

If the colours depend on the values they must be conditionally formmated so use the COUNTIF function as used in the conditional format.


2015-05-21 05:22:46

Kevin Clifford

How do count cells by colour? i.e. If you have a spreadsheet with cells with different colours to denote different values.


2013-09-23 07:02:23

Anne

You can get to "Choose Format From Cell" quicker by clicking the dropdown arrow on the "Format" button in step 5 and selecting "Choose Format From Cell".


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.