Counting Shaded Cells

Written by Allen Wyatt (last updated March 7, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


Excel allows you to apply all sorts of formatting to the cells in your workbook. One of the things you can do is to "shade" cells using a pattern or color. (You do this on the Patterns tab of the Format Cells dialog box.) At some point you may want to know how many cells in a range are shaded.

There is no worksheet formula in Excel that will allow you to count shaded cells. Instead, you must develop your own macro to do this. The following macro is an example of a way to approach this problem. It counts the number of shaded cells in the range of A1 through J20, and places the count in cell A1.

Sub CountColor()
    Dim irow, icol As Integer

    Cells(1, 1) = 0
    For irow = 1 To 20
        For icol = 1 To 10
            If Cells(irow, icol).Interior.ColorIndex _
              <> xlColorIndexNone Then
                Cells(1, 1) = Cells(1, 1) + 1
            End If
        Next icol
    Next irow
End Sub

Notice that the heart of the routine is the comparison that is done between the ColorIndex of each cell and the pre-defined xlColorIndexNone constant. If they are not equal, then the cell has been shaded in some way.

This same basic technique can be easily adapted to a custom function. Notice in the following that the same comparison is done on a cell-by-cell basis:

Function FindShades(a As Range) As Integer
    FindShades = 0
    For Each c In a
        If c.Interior.ColorIndex <> xlColorIndexNone Then
            FindShades = FindShades + 1
        End If
    Next c
End Function

In order to use this function, simply use it in a cell, as a formula, and specify a range in the formula:

= FindShades(B7:E52)

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 (2059) applies to Microsoft Excel 97, 2000, 2002, and 2003.

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

Inserting Signature Lines

How to create signature lines in a Word document.

Discover More

Showing Filter Criteria on a Printout

When you print out a filtered worksheet, you may want some sort of printed record as to what filtering was applied to the ...

Discover More

Age Calculation with Fields

People don't normally think of using fields to do any calculations. Even so, you can use fields to perform a simple ...

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 (menu)

Running a Macro when a Workbook is Closed

One of the automatic macros you can set up in Excel is one that is triggered when a workbook is closed. This tip explains ...

Discover More

Conditionally Displaying a Message Box

You can, from within your macros, easily display a message box containing a message of your choice. If you want to ...

Discover More

Adjusting Values with Formulas

Paste Special is a great tool that allows you to modify the values in a range of cells in your worksheets. You may want, ...

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}] (all 7 characters, in the sequence shown) 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 1 + 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.