Forum Discussion
Excel: Count cells with specific value + colour
Hi there,
I would like to be able to count the number of cells that contain a specific value and are shaded a specific colour. This is for a rota spreadsheet that will be tallying the total number of shifts which have been agreed. The cells that contain "Yes" as a value from a drop down list and are shaded green, should be counted as part of the quarterly total.
I'm assuming that a VBA custom function would be needed for this. I've tried a few but cannot figure out a way to get this to work with both a specific value and colour. I would be very grateful for anyone that is able crack this challenge.
In the example screenshots, it is the cells shaded in green that would need to be counted and the totals appear in column GJ.
Best regards,
Daniel
7 Replies
- Patrick2788Silver Contributor
Let's hope one of Excel's next innovations is a STYLE function to remove the need for vba for this task.
- LorenzoSilver Contributor
++1 With the number of times such question or similar has been raised an alternative to vba should be offered
- LorenzoSilver Contributor
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 workNote 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 <> FFFFFFFunction 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 FunctionOption 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- burningshieldCopper 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.
- LorenzoSilver 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
- Harun24HRBronze Contributor
How the cells are made color? It is via conditional formatting or manually? If the color is made by conditional formatting then we could use same formula to count/some values.
- burningshieldCopper Contributor
The cells are manually highlighted to green. The idea is that individuals can mark the dates on the rota that they would prefer with a "Yes" from the drop down list in each cell. Then management can review and manually highlight in green which ones they have authorised. The authorised cells should then be counted as part of the quarterly total.