Counting cells with a certain color and value

garretthered

New Member
Joined
Feb 7, 2017
Messages
8
I need help trying to get a count of cells that have a specific fill color and certain value. It would be like how many pink cells contain the value 3.
 
I tested that change and the code doesn't seem to be working correctly.
When I call the the function: =CountColorValue(B1:B8)
it returns #VALUE!
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
The CountColorValue you posted is not a Function, it is a Sub Procedure.
You cannot call Sub Procedure like you do a function, you simply run it from the Macro Menu (or attach the code to a button or keyboard shortcut).
 
Last edited:
Upvote 0
My code seems to a flaw in it
When use it returns #VALUE!. So i either messed up a part of the code am using it incorrectly.
 
Upvote 0
I changed it from sub to Function

Function CountColorValue()


Dim numbers As Long, lastrow As Long
Dim rng As Range, c As Range


'Find the last row
lastrow = Range("B" & Rows.Count).End(xlUp).Row
'Set the range you want to search through
Set rng = Range("B1:B" & lastrow)


'Iterate through each cell in the range
For Each c In rng
'If the interior color is 2 (standard pink), not blank and not a number
If c.Interior.ColorIndex = "22" And c.Value = 3 Then '<> "" And Not IsNumeric(c.Value) Then
'Add 1 to numbers
numbers = numbers + 1
End If
Next c


'Message box with the value of numbers, change to display however you'd like
'MsgBox numbers


End Function
 
Upvote 0
Let's take a step back here and make sure we define all the requirements.

What can you tell me about the range you want to run this on?
Will it be the same column every time, or does it need to be dynamic?
Where do you want the results returned to, a specific cell, a message box, a VBA variable?
 
Upvote 0
The count would run for a single column and give the value underneath that column. It would also need to be dynamic, because it will look for a two different colors and at time multiple values (pink - female, cyan - male, shift - 12, 23, 1, 2, 3).
 
Upvote 0

Forum statistics

Threads
1,215,832
Messages
6,127,151
Members
449,366
Latest member
reidel

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