Using COUNTIF with Colors

by Allen Wyatt
(last updated September 21, 2013)

11

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

Updating Document Links

If you establish dynamic links between documents, then you can force Word to update those links whenever you want. How you do ...

Discover More

Searching for All

When you are working on a worksheet (particularly a large one), you may want to search for and possibly copy information from ...

Discover More

Changing the Characteristics of the Recycle Bin

Windows allows you to easily configure the properties associated with the Recycle Bin. This tip provides an overview of what ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Median of Selected Numbers

Need to find a median value in a series of values? It's easy with the MEDIAN function. What isn't as easy is to derive the ...

Discover More

Modifying Proper Capitalization

The PROPER worksheet function is used to change the case of text so that the first letter of each word is capitalized. If ...

Discover More

Returning Item Codes Instead of Item Names

The data validation capabilities of Excel are really handy when you want to limit what is put into a cell. However, you can't ...

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 7 + 6?

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.