Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Shaded Cells in Excel

Status
Not open for further replies.

AGlazer

Programmer
Sep 15, 2002
38
US
Hey guys.

I got an odd question from someone in my office the other day and I wanted to see if anyone had any suggestions. He wants to be able to count the total number of shaded cells in a row and/or column. I'm comfortable using the count functions, but I'm clueless how to approach this to look at shaded cells. Any and all suggestions would be greatly appreciated!

Thanks,

Aaron
 
It can be done with a VBA function, but there are a couple of wrinkles.

Paste this macro in a module in the workbook. If you are new to VBA, Press Alt-F11 then from the menu: Insert/Module. You can then close the VBA editor.
Code:
Function CountShaded(Optional ByVal X As Double = 0) As Double
Dim c As Range
For Each c In ThisWorkbook.ActiveSheet.UsedRange
  If c.Interior.ColorIndex <> xlNone Then CountShaded = CountShaded + 1
Next c
End Function
Now paste this function into any cell:
Code:
  =CountShaded(RAND())
When additional cells are shaded, this does not automatically update. You can press F9 to refresh. On the other hand, anytime the worksheet does recalc, the shaded cells will be re-counted. This could cause a performance problem if the worksheet is very large. (The number of shaded cells is immaterial, the entire worksheet is scanned each time it is recalculated.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top