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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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