Count using Color Function

sjm44

New Member
Joined
Jul 26, 2011
Messages
13
Hello,

I am using Excel 2010 and I'm trying to figure out how to count the number of cells that are filled with a certain colour.

I have browsed the forums and came across a thread on Color Function. That thread told me to use the following code.

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

I am also using the following formula,

=ColorFunction(C1,A1:A22,TRUE)

At the moment when I try to run the formula I get the following message in the code for Module 2, "Complie error: Invalid outside procedure"

I feel I am some way off at the moment and could use a little help to determine how many cells are filled with a certain colour.

Any help would be much appreciated.

Thanks.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If you want to count rather than sum cells you need to change TRUE in the formula to FALSE.

That function will only count manually coloured cells, not cells coloured by Conditional Formatting.
 
Upvote 0
If you want to count rather than sum cells you need to change TRUE in the formula to FALSE.

That function will only count manually coloured cells, not cells coloured by Conditional Formatting.

Ok, so now I have the following...

=ColorFunction(E5,A1:A22,FALSE)

The cells I want to count are filled by using the 'Fill' icon, every time I click away from the formula though it takes me to the Compile Error.

Do I still need to have the Modules if I have this formula?
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top