Using COUNTIF with Colors

by Allen Wyatt
(last updated September 21, 2013)

15

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
http://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:

http://www.extendoffice.com/product/kutools-for-excel.html

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11725) applies to Microsoft Excel 2007, 2010, and 2013.

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

Quicker Multiple Replace Operations

Need to replace a lot of the same characters very, very quickly? Here's a great way to do the replacement.

Discover More

Formatting Raw Data

When you get a bunch of raw data into Excel from an external source, it isn't going to be formatted to your liking. The ...

Discover More

ScreenTips without Hyperlinks

ScreenTips can be a helpful feature in some documents, but adding them also means you need to add a hyperlink. Here's a ...

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)

Calculating the Day of the Year

Need to know what day of the year a certain date is? You can figure it out easily using the formulas in this tip.

Discover More

Using the REPT Function

Excel includes a handy function that allows you to repeat characters or strings of characters. How you use the REPT ...

Discover More

Finding the Date Associated with a Negative Value

When working with data taken from the real world, you often have to determine which certain conditions were met, such as ...

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 8 + 0?

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.