ExcelRibbon.Tips.Net ExcelTips (Ribbon Interface)

Using COUNTIF with Colors

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:


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:


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

Related Tips:

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!


Leave your own comment:

  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*What is 5+3 (To prevent automated submissions and spam.)
           Commenting Terms

Comments for this tip:

Carl    11 Aug 2016, 18:14

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.
Allan     09 Mar 2016, 01:09
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
Allan     08 Mar 2016, 12:16
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
Aemon    30 Jan 2016, 05:33
I have a query
i want to count characters from a column with highlighted cells in excel
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
number of R in highlighted area
number of I in highlighted area
Kevin C    11 Sep 2015, 20:04
What a clear and concise description of how to do something.

ken charlton    01 Sep 2015, 09:19
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
Peter    22 May 2015, 21:25
As in =COUNTIF(A2:A100,">15")
Peter    22 May 2015, 21:21

If the colours depend on the values they must be conditionally formmated so use the COUNTIF function as used in the conditional format.
Kevin Clifford    21 May 2015, 05:22
How do count cells by colour? i.e. If you have a spreadsheet with cells with different colours to denote different values.
Anne    23 Sep 2013, 07:02
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".

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.