Forum Discussion
Excel: Count cells with specific value + colour
Hi
"Then management can review and manually highlight in green"
There are several shades of green. If managers don't always use exactly the same your count won't work
Note that I'm not at all a VBA expert. Easier options might exist
Option 1
Counts cells in range where cells value = yes and cells RGB Hex color <> FFFFFF
Function CountApprovedDates(targetRange As Range)
'
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim countApproved As Long
Set ws = ActiveSheet
With ws
For Each cell In targetRange.Cells
If LCase(cell.Value) = "yes" Then
If Hex(CStr(cell.Interior.Color)) <> "FFFFFF" Then
countApproved = countApproved + 1
End If
End If
Next cell
End With
CountApprovedDates = countApproved
End Function
Option 2
Counts cells in range where cells value = yes and cells RGB Hex color = 50B000 (Green in the Standard Colors)
Function CountApprovedDates(targetRange As Range)
'
Dim ws As Worksheet
Dim rng As Range
Dim cell As Range
Dim countApproved As Long
Set ws = ActiveSheet
With ws
For Each cell In targetRange.Cells
If LCase(cell.Value) = "yes" Then
If Hex(CStr(cell.Interior.Color)) = "50B000" Then
countApproved = countApproved + 1
End If
End If
Next cell
End With
CountApprovedDates = countApproved
End Function
- burningshieldOct 22, 2025Copper Contributor
Hi Lorenzo,
Thank you so much for your helpful suggestions. I'm definitely not a VBA expert either! I tried your option 1 and this seems to work well with any cell colour shading. However, I couldn't get option 2 to work for some reason, even though I specified the exact HEX code in the code, and highlighted the cells with this in the rota. It didn't seem to pick this up for some reason and I'm not sure what I may be doing wrong.
- LorenzoOct 27, 2025Silver Contributor
Hi
Apologies for the delay (I didn't get a notification re. your reply)
For Option2, try the following...Instead of using the Hex value you see in the dialog box :
- Note the Red, Green and Blue values
- In the VBA code change line:
If Hex(CStr(cell.Interior.Color)) = "xxxxxx" Thenwith:
If Hex(CStr(cell.Interior.Color)) = Hex(RGB(0, 176, 80)) Thenreplacing 0, 176, 80 with the Red, Green and Blue values you noted