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:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2059) applies to Microsoft Excel 97, 2000, 2002, and 2003.
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!
Unprotecting a single worksheet is relatively easy. Unprotecting a whole lot of worksheets is harder. Here's how you can ...
Discover MoreWant to know when a workbook was last modified? Want to put that date within the header of your worksheet? Here's how to ...
Discover MoreNeed a way, in a macro, to convert binary numbers into their decimal equivalents? There are two ways you can get the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments