0% found this document useful (0 votes)
55 views3 pages

Counting Colored Cells

This document provides instructions for using a Visual Basic function to count or sum cells with a particular background color within a range. The function takes the color cell, range, and optional boolean to indicate summing. It loops through the range, compares interior colors, and increments a count or sum variable accordingly. Examples demonstrate counting and summing yellow cells in a sample data set.

Uploaded by

cbrittaiv
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views3 pages

Counting Colored Cells

This document provides instructions for using a Visual Basic function to count or sum cells with a particular background color within a range. The function takes the color cell, range, and optional boolean to indicate summing. It loops through the range, compares interior colors, and increments a count or sum variable accordingly. Examples demonstrate counting and summing yellow cells in a sample data set.

Uploaded by

cbrittaiv
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

The following code can help you count and sum the cells with a certain background color,

please do as this: 1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window. 2. Click Insert > Module, and paste the following code in the Module Window. Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean) Dim rCell As Range Dim lCol As Long Dim vResult lCol = rColor.Interior.ColorIndex If SUM = True Then For Each rCell In rRange If rCell.Interior.ColorIndex = lCol Then vResult = WorksheetFunction.SUM(rCell, vResult) End If Next rCell Else For Each rCell In rRange If rCell.Interior.ColorIndex = lCol Then vResult = 1 + vResult End If Next rCell End If ColorFunction = vResult End Function 3. Then save the code, and apply the following formula: Count the colored cells: =colorfunction(A,B:C,FALSE) Sum the colored cells: =colorfunction(A,B:C,TRUE) A: is the cell with the particular background color you want to calculate the count and sum. B:C: is the cell range where you want to calculate the count and sum. 4. Take the following screenshot for example, enter the formula =colorfunction(A1,A1:D7,FALSE) to count the yellow cells. And use the formula =colorfunction(A1,A1:D7,TRUE) to sum the yellow cells. See screenshot:

5. If you want to count and sum other colored cells, please repeat the step 4. Then you will get the following results:

You might also like