Forum Discussion

Deleted's avatar
Deleted
Oct 12, 2017

How to count and sum "Condtional formatting" cells by color in Excel 2010?

I installed few add-ins for counting "Conditional formatted" cells but all are generating an instant (one-time) numeric result by activating that command.

 

Also, I tried a code which gives an instant counting of 1 picked color without a total sum of all other colors -in my case 12 colors in a matrix. ("How to use the code to count colored cells and sum their values" - https://www.ablebits.com/office-addins-blog/2013/12/12/count-sum-by-color-excel/ )

 
My question is: Is there any functional code (or other aproach) which can count and sum all cells by "Conditional formatted" colors, on one sheet, and that the generated result is linked through common formulas? That can be updated/refreshed/code run during the work process as a complete overview of colors of a matrix (and not by picking every color every time as I mentioned).
 
Thanks in advance!

 
  • I came across this post being unanswered. so here is a User Defined Function in VBA

     

    to SUM 

     

    use this UDF 

    Function SumConditionColorCells(CellsRange As Range, ColorRng As Range)
    Dim Bambo As Boolean
    Dim dbw As String
    Dim CFCELL As Range
    Dim CF1 As Single
    Dim CF2 As Double
    Dim CF3 As Long
    Bambo = False
    For CF1 = 1 To CellsRange.FormatConditions.Count
    If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then
    Bambo = True
    Exit For
    End If
    Next CF1
    CF2 = 0
    CF3 = 0
    If Bambo = True Then
    For Each CFCELL In CellsRange
    dbw = CFCELL.FormatConditions(CF1).Formula1
    dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1)
    dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1))
    If Evaluate(dbw) = True Then CF2 = CF2 + CFCELL.Value
    CF3 = CF3 + 1
    Next CFCELL
    Else
    SumConditionColorCells = "NO-COLOR"
    Exit Function
    End If
    SumConditionColorCells = CF2
    End Function

     

    if you want to Count instead of SUM then use the below UDF

     

    Function COUNTConditionColorCells(CellsRange As Range, ColorRng As Range)
    Dim Bambo As Boolean
    Dim dbw As String
    Dim CFCELL As Range
    Dim CF1 As Single
    Dim CF2 As Double
    Dim CF3 As Long
    Bambo = False
    For CF1 = 1 To CellsRange.FormatConditions.Count
    If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then
    Bambo = True
    Exit For
    End If
    Next CF1
    CF2 = 0
    CF3 = 0
    If Bambo = True Then
    For Each CFCELL In CellsRange
    dbw = CFCELL.FormatConditions(CF1).Formula1
    dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1)
    dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1))
    If Evaluate(dbw) = True Then CF2 = CF2 + 1
    CF3 = CF3 + 1
    Next CFCELL
    Else
    COUNTConditionColorCells = "NO-COLOR"
    Exit Function
    End If
    COUNTConditionColorCells = CF2
    End Function
    

     

    these solutions were provided to the similar question asked by other Excel users and worked for them.

     

    For more detail here is the link for that answer 

  • Jamil's avatar
    Jamil
    Bronze Contributor

    I came across this post being unanswered. so here is a User Defined Function in VBA

     

    to SUM 

     

    use this UDF 

    Function SumConditionColorCells(CellsRange As Range, ColorRng As Range)
    Dim Bambo As Boolean
    Dim dbw As String
    Dim CFCELL As Range
    Dim CF1 As Single
    Dim CF2 As Double
    Dim CF3 As Long
    Bambo = False
    For CF1 = 1 To CellsRange.FormatConditions.Count
    If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then
    Bambo = True
    Exit For
    End If
    Next CF1
    CF2 = 0
    CF3 = 0
    If Bambo = True Then
    For Each CFCELL In CellsRange
    dbw = CFCELL.FormatConditions(CF1).Formula1
    dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1)
    dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1))
    If Evaluate(dbw) = True Then CF2 = CF2 + CFCELL.Value
    CF3 = CF3 + 1
    Next CFCELL
    Else
    SumConditionColorCells = "NO-COLOR"
    Exit Function
    End If
    SumConditionColorCells = CF2
    End Function

     

    if you want to Count instead of SUM then use the below UDF

     

    Function COUNTConditionColorCells(CellsRange As Range, ColorRng As Range)
    Dim Bambo As Boolean
    Dim dbw As String
    Dim CFCELL As Range
    Dim CF1 As Single
    Dim CF2 As Double
    Dim CF3 As Long
    Bambo = False
    For CF1 = 1 To CellsRange.FormatConditions.Count
    If CellsRange.FormatConditions(CF1).Interior.ColorIndex = ColorRng.Interior.ColorIndex Then
    Bambo = True
    Exit For
    End If
    Next CF1
    CF2 = 0
    CF3 = 0
    If Bambo = True Then
    For Each CFCELL In CellsRange
    dbw = CFCELL.FormatConditions(CF1).Formula1
    dbw = Application.ConvertFormula(dbw, xlA1, xlR1C1)
    dbw = Application.ConvertFormula(dbw, xlR1C1, xlA1, , ActiveCell.Resize(CellsRange.Rows.Count, CellsRange.Columns.Count).Cells(CF3 + 1))
    If Evaluate(dbw) = True Then CF2 = CF2 + 1
    CF3 = CF3 + 1
    Next CFCELL
    Else
    COUNTConditionColorCells = "NO-COLOR"
    Exit Function
    End If
    COUNTConditionColorCells = CF2
    End Function
    

     

    these solutions were provided to the similar question asked by other Excel users and worked for them.

     

    For more detail here is the link for that answer 

  • Hi, this kind of question is better communicated with a document, stating the problems in short form in the worksheet. Thanks for sharing...
  • vc337's avatar
    vc337
    Copper Contributor

    Hi Jamil , 

     

    I have a similar question, however, when I applied the codes it only counts the cell by column, which I also need it to be counted by row. I wonder if it is because each column has a different condition formatting criteria.  Could you please help? Please see the attached file. 

     

    Thanks!!

    • Jamil's avatar
      Jamil
      Bronze Contributor

      vc337 

       

      Hi,  please read my earlier posts.  the UDF do not work if the CF is set using built-in conditions.  UDF works only when you set conditions with formula. "Use a formula to determine which cell to format"

       

      if you set your CF using formula, then it works.

      • llRodrigo's avatar
        llRodrigo
        Copper Contributor

        Jamil 

        Hi could you please check my file, i can't make it work, it gives me the error:
        #VALOR!

        English is not my first language, so i tried to make it work reading the posts here. Thanks if you can take a look into my file.

  • AK_Arun's avatar
    AK_Arun
    Copper Contributor


    Hi There,

    How to count conditionally formatted red color cell?

    I've tried many codes but still not working...

    Range Cell : D40:D70

    Sample Color : K40

    Count Cell : L40

    Thanks.

Resources