Count a list of cells with the same background color

后端 未结 5 1284
时光取名叫无心
时光取名叫无心 2020-12-19 01:22

Each cell contains some text and a background color. So I have some cells that are blue and some that are red. What function do I use to count the number of red cells?

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 01:54

    I just created this and it looks easier. You get these 2 functions:

    =GetColorIndex(E5)  <- returns color number for the cell
    

    from (cell)

    =CountColorIndexInRange(C7:C24,14) <- returns count of cells C7:C24 with color 14
    

    from (range of cells, color number you want to count)

    example shows percent of cells with color 14

    =ROUND(CountColorIndexInRange(C7:C24,14)/18, 4 )
    

    Create these 2 VBA functions in a Module (hit Alt-F11)

    open + folders. double-click on Module1

    Just paste this text below in, then close the module window (it must save it then):

    Function GetColorIndex(Cell As Range)
      GetColorIndex = Cell.Interior.ColorIndex
    End Function
    
    Function CountColorIndexInRange(Rng As Range, TestColor As Long)
      Dim cnt
      Dim cl As Range
      cnt = 0
    
      For Each cl In Rng
        If GetColorIndex(cl) = TestColor Then
          Rem Debug.Print ">" & TestColor & "<"
          cnt = cnt + 1
        End If
      Next
    
      CountColorIndexInRange = cnt
    
    End Function
    

提交回复
热议问题