add up things by distinguishing them by color

%3CLINGO-SUB%20id%3D%22lingo-sub-1331578%22%20slang%3D%22en-US%22%3Eadd%20up%20things%20by%20distinguishing%20them%20by%20color%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1331578%22%20slang%3D%22en-US%22%3E%3CP%3EI%20want%20to%20make%20some%20calculations.%20but%20the%20calculations%20are%20distinguished%20by%20color.%20I%20want%20to%20add%20them%20by%20color.%20like%20the%20example%20in%20the%20picture%20I%20added.%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Screenshot%20(135).png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F186068iA790A787B7942004%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22ASK%22%20alt%3D%22description%20on%20image%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Edescription%20on%20image%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1331578%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1336106%22%20slang%3D%22en-US%22%3ERe%3A%20add%20up%20things%20by%20distinguishing%20them%20by%20color%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1336106%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F633917%22%20target%3D%22_blank%22%3E%40TheFlatFace%3C%2FA%3E%26nbsp%3Bhello%20there%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUnfortunately%20you%20can't%20do%20this%20with%20native%20functions.%20You%20can%20set%20a%20filter%20range%20on%20your%20data%20and%20filter%20by%20color%20-%20which%20is%20now%20available%20in%20Excel%20Online%20as%20well.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20being%20said%2C%20there%20is%20a%20VBA%20solution.%20You%20would%20need%20to%20install%20this%20code%20into%20your%20workbook%20and%20it%20would%20only%20work%20there%20(other%20solutions%20exist%20to%20make%20it%20available%20in%20other%20workbooks%20if%20you'd%20like).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20code%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EPublic%20Function%20SumByFontColor(%20_%0A%20%20%20%20ByVal%20SumRange%20As%20Range%2C%20_%0A%20%20%20%20ByVal%20FontColorCell%20As%20Range%20_%0A%20%20%20%20)%20As%20Double%0A%20%20%20%20%0A%20%20%20%20Dim%20Cell%20As%20Range%0A%20%20%20%20Dim%20SourceColorIndex%20As%20Long%0A%20%20%20%20%0A%20%20%20%20SourceColorIndex%20%3D%20FontColorCell.Font.ColorIndex%0A%20%20%20%20%0A%20%20%20%20For%20Each%20Cell%20In%20SumRange%0A%20%20%20%20%20%20%20%20If%20Cell.Font.ColorIndex%20%3D%20SourceColorIndex%20And%20IsNumeric(Cell.Value)%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20SumByFontColor%20%3D%20SumByFontColor%20%2B%20Cell.Value%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20Cell%0A%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20install%20the%20code%3A%3C%2FP%3E%3COL%3E%3CLI%3EOpen%20the%20Visual%20Basic%20Editor%20(VBE)%3COL%3E%3CLI%3EALT%2BF11%20should%20do%20the%20trick%3C%2FLI%3E%3CLI%3EOr%2C%20click%20on%20Developer%20tab%20and%20click%20Visual%20Basic.%20If%20Developer%20tab%20not%20showing%3A%3COL%3E%3CLI%3ERight-click%20the%20ribbon%2C%20choose%20Customize%20the%20Ribbon%3C%2FLI%3E%3CLI%3EOn%20the%20right%20list%2C%20check%20the%20box%20next%20to%20Developer%3C%2FLI%3E%3CLI%3EClick%20OK%3C%2FLI%3E%3C%2FOL%3E%3C%2FLI%3E%3C%2FOL%3E%3C%2FLI%3E%3CLI%3EFind%20your%20project%20in%20the%20Project%20Explorer%3COL%3E%3CLI%3EIf%20not%20showing%2C%20press%20CTRL%2BR%3C%2FLI%3E%3C%2FOL%3E%3C%2FLI%3E%3CLI%3ERight-click%20your%20project%2C%20select%20Insert%20%26gt%3B%20Module%3C%2FLI%3E%3CLI%3EPaste%20the%20code%20in%20the%20code%20pane%3C%2FLI%3E%3CLI%3EClose%20the%20VBE%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUsage%3A%3C%2FP%3E%3CP%3E%3DSumByFontColor(RangeToSum%2CCellWithFontColorToSumBy)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExample%3A%3C%2FP%3E%3CP%3EYou%20want%20to%20sum%20range%20A1%3AA10.%20Cell%20B1%20contains%20a%20cell%20which%20has%20the%20font%20color%20you%20want%20to%20sum%20by%3A%3C%2FP%3E%3CP%3E%3DSumByFontColor(A1%3AA10%2CB1)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENote%20this%20formula%20will%20not%20update%20itself%20if%20you%20change%20cell%20colors.%20That%20action%20doesn't%20trigger%20an%20update%20in%20the%20calc%20tree%20of%20any%20kind.%20You%20could%20add%20%22Application.Volatile%20%3D%20True%22%20to%20the%20function%20but%20that%20would%20only%20help%20in%20limited%20cases.%20This%20is%20not%20perfect%20solution.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAs%20a%20side%20note%2C%20I%20would%20recommend%20not%20using%20things%20like%20%22222k%22%20in%20a%20cell%20as%20a%20value%2C%20as%20Excel%20will%20see%20that%20as%20text%20and%20not%20be%20able%20to%20do%20math%20on%20it.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Visitor

I want to make some calculations. but the calculations are distinguished by color. I want to add them by color. like the example in the picture I added. 

description on imagedescription on image

1 Reply
Highlighted

@TheFlatFace hello there,

 

Unfortunately you can't do this with native functions. You can set a filter range on your data and filter by color - which is now available in Excel Online as well.

 

That being said, there is a VBA solution. You would need to install this code into your workbook and it would only work there (other solutions exist to make it available in other workbooks if you'd like).

 

The code:

Public Function SumByFontColor( _
    ByVal SumRange As Range, _
    ByVal FontColorCell As Range _
    ) As Double
    
    Dim Cell As Range
    Dim SourceColorIndex As Long
    
    SourceColorIndex = FontColorCell.Font.ColorIndex
    
    For Each Cell In SumRange
        If Cell.Font.ColorIndex = SourceColorIndex And IsNumeric(Cell.Value) Then
            SumByFontColor = SumByFontColor + Cell.Value
        End If
    Next Cell

End Function

 

To install the code:

  1. Open the Visual Basic Editor (VBE)
    1. ALT+F11 should do the trick
    2. Or, click on Developer tab and click Visual Basic. If Developer tab not showing:
      1. Right-click the ribbon, choose Customize the Ribbon
      2. On the right list, check the box next to Developer
      3. Click OK
  2. Find your project in the Project Explorer
    1. If not showing, press CTRL+R
  3. Right-click your project, select Insert > Module
  4. Paste the code in the code pane
  5. Close the VBE

 

Usage:

=SumByFontColor(RangeToSum,CellWithFontColorToSumBy)

 

Example:

You want to sum range A1:A10. Cell B1 contains a cell which has the font color you want to sum by:

=SumByFontColor(A1:A10,B1)

 

Note this formula will not update itself if you change cell colors. That action doesn't trigger an update in the calc tree of any kind. You could add "Application.Volatile = True" to the function but that would only help in limited cases. This is not perfect solution.

 

As a side note, I would recommend not using things like "222k" in a cell as a value, as Excel will see that as text and not be able to do math on it.