Count Cells Based on Cell Color

lichldo

Board Regular
Joined
Apr 19, 2022
Messages
65
Office Version
  1. 365
Platform
  1. MacOS
I found the following code online to be able to count how many cells are in a certain color, however its only returning #NAME

So I'm using this VBA -
Function GetColorCount(CountRange As Range, CountColor As Range, TheValue As Variant)
Dim CountColorValue As Long
Dim TotalCount As Long
Dim rCell As Range
CountColorValue = CountColor.Interior.Color
Set rCell = CountRange
For Each rCell In CountRange
If rCell.Interior.Color = CountColorValue And rCell.Value = TheValue Then
TotalCount = TotalCount + 1
End If
Next rCell
GetColorCount = TotalCount
End Function

And then in the cell I want the sum total in, I have =GetColorCount($B$2:$B$25,L1)
Where the range I want to total is B2:B25 and L1 has the background color I want to count

This is not working
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Upvote 0
The code you are using has 3 arguments:
Function GetColorCount(CountRange As Range, CountColor As Range, TheValue As Variant)

and your formula only has two:
=GetColorCount($B$2:$B$25,L1)

So you do not seem to be using the formula as created.
I do not think you are using the right function for what you want to do.

Did you get this function from here: https://answers.microsoft.com/en-us...criteria/12819f9b-4666-40d6-9536-b16ee610b76f
Note what it says in the original question:
Following instructions on how to create a UDF to count colors worked perfectly but I also would like it to count the colors according to specific text in the cell.

If you are only wanting to count based on color with no other conditions, then you do not want to use that version.
Instead, you should be using the original version in that question:
VBA Code:
Function GetColorCount(CountRange As Range, CountColor As Range)
Dim CountColorValue As Integer
Dim TotalCount As Integer
CountColorValue = CountColor.Interior.ColorIndex
Set rCell = CountRange
For Each rCell In CountRange
  If rCell.Interior.ColorIndex = CountColorValue Then
    TotalCount = TotalCount + 1
  End If
Next rCell
GetColorCount = TotalCount
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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