Forum Discussion

romancristiani's avatar
romancristiani
Copper Contributor
Mar 05, 2023
Solved

Counting Cells by Color with conditional formatting

Each row represents a location. Each column is a metric that we track for each location. The metrics and goals are the same for each location. Each cell changes color based on conditional formatting. The cells of each column have the same conditions but the conditions change per column. The cells change constantly. New tabs are created monthly for each district. I need the total of the like colored cells to compute to the matching rows of the color coded columns below. Is this possible?

 

 

HansVogelaar

  • HansVogelaar's avatar
    HansVogelaar
    Mar 05, 2023

    romancristiani 

    Here is the VBA code I came up with:

    Function CountColor(rng As Range, colorcell As Range) As Long
        Dim cell As Range
        Dim clr As Long
        clr = colorcell.Interior.Color
        For Each cell In rng
            If Evaluate("GetColor(" & cell.Address(External:=True) & ")") = clr Then
                CountColor = CountColor + 1
            End If
        Next cell
    End Function
    
    Function GetColor(cell As Range) As Long
        GetColor = cell.DisplayFormat.Interior.Color
    End Function

    The formula in B20 is

     

    =IF($A20="", "", CountColor($B8:$P8, B$19))

     

    This can be filled to the right to D20, ten down to row 25.

    I have attached the workbook to a PM to you. You'll have to allow macros when you open it.

16 Replies

    • HansVogelaar's avatar
      HansVogelaar
      MVP

      romancristiani 

      Here is the VBA code I came up with:

      Function CountColor(rng As Range, colorcell As Range) As Long
          Dim cell As Range
          Dim clr As Long
          clr = colorcell.Interior.Color
          For Each cell In rng
              If Evaluate("GetColor(" & cell.Address(External:=True) & ")") = clr Then
                  CountColor = CountColor + 1
              End If
          Next cell
      End Function
      
      Function GetColor(cell As Range) As Long
          GetColor = cell.DisplayFormat.Interior.Color
      End Function

      The formula in B20 is

       

      =IF($A20="", "", CountColor($B8:$P8, B$19))

       

      This can be filled to the right to D20, ten down to row 25.

      I have attached the workbook to a PM to you. You'll have to allow macros when you open it.

      • hchopra06's avatar
        hchopra06
        Copper Contributor

        HansVogelaar I am impressed with your skills. I'm in a pickle at work. I am a quality manager and I have a data file where I am trying to count the color of cells in each column. They are colored by conditional formatting.

        https://docs.google.com/spreadsheets/d/1qzoLFtQDR8fWPxq10xM9C5FADP4xX_o5p2Nzcy2XHJA/edit?usp=sharing

         

        I hope you help me out ❤️

      • romancristiani's avatar
        romancristiani
        Copper Contributor
        I need to total number of Reds, Yellows and Greens in the respective color column below per location. Also Rows 4-7 that don't show in the photo above were deleted.

Resources