Using COUNTIF with Colors

by Allen Wyatt
(last updated September 21, 2013)

12

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

Hiding a Hyperlink on a Printout

Hyperlinks can be real handy in a workbook, but you may not always want them visible when you send the workbook to the ...

Discover More

Determining If a Number is Odd or Even

Need to know if a number in a macro is odd or even? It's easy to figure out with the simple calculation shown in this tip.

Discover More

Determining How Many Windows are Open

You can open multiple documents at the same time in Word, and each document occupies its own document window. Here's a way ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

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

The EDATE Function

Want to figure a date a certain number of months in the future or past? The EDATE function may be just what you need for your ...

Discover More

Making VLOOKUP Case Sensitive

The VLOOKUP function, like other lookup functions in Excel, is not case sensitive. In other words, it doesn't matter whether ...

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 four less than 9?

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.