Forum Discussion

burningshield's avatar
burningshield
Copper Contributor
Oct 17, 2025

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

  • Patrick2788's avatar
    Patrick2788
    Silver Contributor

    Let's hope one of Excel's next innovations is a STYLE function to remove the need for vba for this task.

    • Lorenzo's avatar
      Lorenzo
      Silver Contributor

      ++1 With the number of times such question or similar has been raised an alternative to vba should be offered

  • Lorenzo's avatar
    Lorenzo
    Silver 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 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

     

    • burningshield's avatar
      burningshield
      Copper 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. 

       

       

      • Lorenzo's avatar
        Lorenzo
        Silver 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" Then

        with:

        If Hex(CStr(cell.Interior.Color)) = Hex(RGB(0, 176, 80)) Then

        replacing 0, 176, 80 with the Red, Green and Blue values you noted

  • Harun24HR's avatar
    Harun24HR
    Bronze 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.

    • burningshield's avatar
      burningshield
      Copper 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. 

Resources