Forum Discussion

Hr_Justino's avatar
Hr_Justino
Copper Contributor
May 04, 2021
Solved

Count and Sum by Background Color, with color set by both Manual and conditional formatting

Hello, I have made an add-in to count and sum when colors are manually set.
However when Conditional Formatting is used I.E. Setting Equal to 1 to have a red background.
The VBA code ignores the Conditional format and instead counts or sums as if the color didn't change. 
Is there some VBA code that can count/Sum by background color which also includes conditional formatting changes.
(The conditional Format is set to Equal to 1  background is red)
As in picture the red field are counted when in reality.
I dont think they should since their color is red and not yellow or orange.
Hope this makes sense to someone.

  • Hr_Justino 

    In VBA, you can use range.DisplayFormat.Interior.ColorIndex to return the color of a cell as displayed, whether through direct formatting or through conditional formatting. But unfortunately, DisplayFormat does not work when used directly in a user-defined function, so you have to use a trick.

    Create a function like this:

    Function DColorIndex(r As Range) As Long
        DColorIndex = r.DisplayFormat.Interior.ColorIndex
    End Function

    You can then use these functions:

    Function ColourCount(cel As Range, ran As Range) As Long
        Dim colo As Long
        Dim c As Range
        Dim cou As Long
        colo = cel.Interior.ColorIndex
        For Each c In ran
            If ran.Parent.Evaluate("DColorIndex(" & c.Address & ")") = colo Then
                cou = cou + 1
            End If
        Next c
        ColourCount = cou
    End Function
    
    Function ColourSum(cel As Range, ran As Range) As Double
        Dim colo As Long
        Dim c As Range
        Dim colsum As Double
        colo = cel.Interior.ColorIndex
        For Each c In ran
            If ran.Parent.Evaluate("DColorIndex(" & c.Address & ")") = colo Then
                colsum = colsum + c.Value
            End If
        Next c
        ColourSum = colsum
    End Function

    By the way 1: Excel supports many more colors than just the 56 palette colors. It might be better to use the Color property instead of the ColorIndex property.

     

    By the way 2: in the future, please post the code instead of a screenshot of the code! That saves the person trying to help you a lot of time.

  • Hr_Justino 

    In VBA, you can use range.DisplayFormat.Interior.ColorIndex to return the color of a cell as displayed, whether through direct formatting or through conditional formatting. But unfortunately, DisplayFormat does not work when used directly in a user-defined function, so you have to use a trick.

    Create a function like this:

    Function DColorIndex(r As Range) As Long
        DColorIndex = r.DisplayFormat.Interior.ColorIndex
    End Function

    You can then use these functions:

    Function ColourCount(cel As Range, ran As Range) As Long
        Dim colo As Long
        Dim c As Range
        Dim cou As Long
        colo = cel.Interior.ColorIndex
        For Each c In ran
            If ran.Parent.Evaluate("DColorIndex(" & c.Address & ")") = colo Then
                cou = cou + 1
            End If
        Next c
        ColourCount = cou
    End Function
    
    Function ColourSum(cel As Range, ran As Range) As Double
        Dim colo As Long
        Dim c As Range
        Dim colsum As Double
        colo = cel.Interior.ColorIndex
        For Each c In ran
            If ran.Parent.Evaluate("DColorIndex(" & c.Address & ")") = colo Then
                colsum = colsum + c.Value
            End If
        Next c
        ColourSum = colsum
    End Function

    By the way 1: Excel supports many more colors than just the 56 palette colors. It might be better to use the Color property instead of the ColorIndex property.

     

    By the way 2: in the future, please post the code instead of a screenshot of the code! That saves the person trying to help you a lot of time.

    • rewesh's avatar
      rewesh
      Copper Contributor
      Thanks, your code works .. After 1 h of searching the internet .. thanks a lot ..Is there a way to make this dynamic or update automatically?
    • Hr_Justino's avatar
      Hr_Justino
      Copper Contributor
      Hi HansVogelaar
      I have fixed the issue With ColourSum not working and in turn have made it so DColorindex would not be needed.
      Here is the Code that does all I require (I Changed Double to Long in the Function ColourSum )
      ________________________________________________________________________________________
      Function ColourCount(cel As Range, ran As Range) As Long
      Dim colo As Long
      Dim c As Range
      Dim cou As Long
      colo = cel.Interior.ColorIndex
      For Each c In ran
      If ran.Parent.Evaluate("DColorIndex(" & c.Address & ")") = colo Then
      cou = cou + 1
      End If
      Next c
      ColourCount = cou
      End Function
      ________________________________________________________________________________________________
      Function ColourSum(cel As Range, ran As Range) As Long
      Dim colo As Long
      Dim c As Range
      Dim colsum As Long
      colo = cel.Interior.ColorIndex
      For Each c In ran
      If ran.Parent.Evaluate("DColorIndex(" & c.Address & ")") = colo Then
      colsum = colsum + c.Value
      End If
      Next c
      ColourSum = colsum
      End Function
      _________________________________________________________________________________________
      Many Thanks HansVogelaar
      • HansVogelaar's avatar
        HansVogelaar
        MVP

        Hr_Justino 

        1. Your version of ColourSum won't sum numbers with decimals correctly - it will round every number to a whole number.
        2. Both functions still require DColorIndex to be present in the module - if you remove the code for DColorIndex, ColourCount and ColourSum would fail.
    • Hr_Justino's avatar
      Hr_Justino
      Copper Contributor
      Hi HansVogelaar
      I have Input the code, and removed my old code to a text file on my desktop.
      I run into issues when I try to run DColorIndex. I dont understand how to make the function work.
      Sorry this might be a bit basic.

      Your ColourCount works lovely.

      But I also had trouble wit ColourSum.
      The debugger show up on the line "For Each c in ran"
      If you could explain how to use the function DColorIndex that would be great.
      I assumed I would write the function and choose the control color and then hit enter.
      "doesn't work"
  • JKPieterse's avatar
    JKPieterse
    Silver Contributor
    You can do this, but in general I advise not to use formatting as a value to do calculations with. It is far better to have your users type a value (text or number doesn't matter much) into a cell. You can then use a conditional formatting rule to color those cells accordingly. And you can then simply use SUMIFS or COUNTIFS functions to do the summary, or even better, use a pivottable.

Resources