logo elektroda
logo elektroda
X
logo elektroda

How to Sum Cells in Excel Based on Font Color: Black Paid, Red Unpaid - Easy Method

doncap 15804 12
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 17008900
    doncap
    Level 1  
    Posts: 1
    Hello
    I have a datasheet, the table has only two font colors, black and red, the color is changed manually (black paid, red unpaid).
    I am looking for a way to easily sum cells in a column based on a defined font color,
    Thank you for your help.
    Dariusz
  • ADVERTISEMENT
  • #2 17008933
    adamas_nt
    VIP Meritorious for electroda.pl
    Posts: 5320
    Help: 1508
    Rate: 658
    View Here .
  • ADVERTISEMENT
  • #4 17011090
    dariuszb09
    Level 2  
    Posts: 4
    Rate: 3
    Hello !!!
    I found something like this:
    Code: VBScript
    Log in, to see the code

    The function in the cell looks like this (= SumCellsByFontColor (search range; cell with font color)
    To update the result of F9
    And now I have a question. How to combine two functions - to sum by font color (shown above) and to sum by cell background color (shown below) so that it sums up by background and font color.
    Code: VBScript
    Log in, to see the code

    Regards Darek.
  • #5 17011172
    lanzul
    Level 30  
    Posts: 1023
    Help: 199
    Rate: 123
    Insert the "body" one into another, change the name, set as 'Variant', remove duplicate declarations, add separate variables for the font and background - maybe as 'Double', because if you select a few billion cells, 'Long' will not make - assign for the result of the function, a combination of two sums with some determinants as to the font, and as to the background and will work.
  • ADVERTISEMENT
  • #6 17011358
    dariuszb09
    Level 2  
    Posts: 4
    Rate: 3
    Hello !!!
    Sorry but
    Quote:
    Declarations, variables, 'Double', 'Long'
    it's black magic, for me Excel ends around = if i = search. vertically / horizontally :)
    Anyway, I came to this myself and I am still recognized at work as a SPECIALIST from Excel. I know very little of my knowledge in this subject and VBA is Mary's magic for me, that's why I ask magicians SPECIALISTS for help :)
  • ADVERTISEMENT
  • #7 17012263
    lanzul
    Level 30  
    Posts: 1023
    Help: 199
    Rate: 123
    I'm not a specialist or magician ... :) ... but if you think about it, you could, for example, make something like this:
    Czcionka-T...-Ilość.zip (9.55 kB)You must be logged in to download this attachment.
  • #8 17012890
    JacekCz
    Level 42  
    Posts: 8670
    Help: 760
    Rate: 1460
    in my opinion, the problem is wrong from the beginning, the color should result from some basic data, and the same data should result from the sum.
  • #9 17013269
    dariuszb09
    Level 2  
    Posts: 4
    Rate: 3
    Hello !!!
    "Lanzul"
    I know that you devoted your time to this function and the spreadsheet but that was not what I meant. I want it to sum (values) only e.g. cells with a blue background and yellow font,
    and separately with purple background and yellow font and separately green background and yellow font or blue background and red font, blue background and green font etc.
    font and background color are best to be given as in the functions above in separate cells.
    Regards Darek.
  • #10 17013693
    lanzul
    Level 30  
    Posts: 1023
    Help: 199
    Rate: 123
    dariuszb09 wrote:
    ... but that's not what I meant. I want it to sum (values) only e.g. cells with a blue background and a yellow font ...

    As you write, they understand you that way - is there no "no" in the above sentence or is there too much "only"?

    dariuszb09 wrote:
    ... I want it to sum (values) (? ...?) e.g. cells with a blue background and a yellow font, and separately with a purple background and a yellow font and separately a green background and a yellow font, or a blue background and a red font, a blue background and green font etc. The font color and background should ideally be given as in the functions above in separate cells

    1. And how do I add up? That's right, you have patterns in separate cells ... :) ... only that the disadvantage of this function is that it does it based on a pattern (everyone does this), which you must select, which is in a cell, and which is not entered in the function call as an argument of "enter the amount of blue with yellow ".

    2. You entered the "problem" yes and no different, you gave the "material" for processing, read what you wrote ... you did not specify other criteria, and I do not intend to guess ... :D ...

    3. And then what do you mean by "sum" - the content of cells meeting the criteria? The number of these cells? By selecting the appropriate optional numeric argument you designate this function for what to count. Just as I write, the function does it after the pattern taken from the pattern cell, and not on the basis of manually entered arguments of the type vb blue, vb yellow, vb white, etc.

    4. If you think about the "automatic" recognition of colors by a function, without specifying its pattern ("in words", indicating the range of conditions), then ... :D ... how many colors do you think she should recognize?
    For RGB there are probably 16 777 216 (for other systems, e.g. CMYK is 4 294 967 296) ... :) ...
    In old Excel, 56 are basic (+ 1 "colorless") - Check how much you have in your new ... :D ...:
    Code: text
    Log in, to see the code


    Use links from adamas_nt and Zbych034 , exercise a little ... cells ... :) ...
  • #11 17013903
    adamas_nt
    VIP Meritorious for electroda.pl
    Posts: 5320
    Help: 1508
    Rate: 658
    My way, both conditions of both found functions must be met.
    Modified, it will just look:
    Code: VBScript
    Log in, to see the code
  • #12 17014089
    dariuszb09
    Level 2  
    Posts: 4
    Rate: 3
    Hello !!!
    "Adamas_nt"
    Many thanks for helping a layman in the field of VBA.
    I checked the work it should. Thanks again :)
    Regards Darek.
  • #13 21313586
    pseudocfoch
    Level 2  
    Posts: 112
    Rate: 1
    Check for Python's XlsxWriter library.

Topic summary

✨ The discussion addresses how to sum Excel cells based on font color, specifically distinguishing between black (paid) and red (unpaid) font colors. The primary solution involves using VBA custom functions to sum cell values by font color. A provided VBA function, SumCellsByFontColor, iterates through a range and sums values of cells matching a reference cell's font color. Further enhancements include combining font color and background color criteria in a single function, allowing sums based on both font and fill colors simultaneously. The combined function compares both the Interior.Color and Font.Color properties of cells against a reference cell and sums matching cells. The approach requires manual color setting and uses Application.Volatile to update results with recalculation (F9). Additional suggestions emphasize that color-based sums should ideally derive from underlying data rather than manual formatting. A brief mention is made of Python's XlsxWriter library as an alternative tool for Excel file manipulation. The discussion highlights practical VBA coding techniques for color-based conditional summation in Excel spreadsheets.
Generated by the language model.

FAQ

TL;DR: Need to sum Excel cells by color? Use a 2-criteria VBA UDF that checks both Font.Color and Interior.Color; “both conditions must be met.” Paste the code, call it in a cell, press F9 to refresh. [Elektroda, adamas_nt, post #17013903]

Why it matters: This solves “how do I sum paid (black) vs unpaid (red) by color?” for non-coders and power users alike.

Quick Facts

How do I sum values by font color only (e.g., black = paid, red = unpaid)?

Add the posted UDF SumCellsByFontColor. Use =SumCellsByFontColor(A2:A200; C1), where C1 holds a sample cell with the target font color. Press F9 if the total does not refresh after manual recoloring. This approach mirrors the original thread solution and is simple for non-coders. [Elektroda, dariuszb09, post #17011090]

How do I sum only cells that match both background and font color?

Use the modified UDF that checks both Interior.Color and Font.Color from the same reference cell. “Both conditions must be met.” Call it like =SumCellsByColor(A2:A200; C1), where C1 has the sample background and font. This enforces an AND match across the two attributes. [Elektroda, adamas_nt, post #17013903]

What does the formula look like and how do I refresh the result?

Example: =SumCellsByColor(A2:A200; C1). The first argument is your range; the second is the pattern cell. The UDF is volatile, but manual recoloring might not trigger recalc. Press F9 to force an update after changing colors. [Elektroda, dariuszb09, post #17011090]

Why do experts caution against basing sums on colors alone?

Color is presentation, not data. The thread notes that underlying data should drive both the color and the sum. Consider adding a Paid/Unpaid flag, then using SUMIFs and conditional formatting for the color. This keeps logic robust and auditable. [Elektroda, JacekCz, post #17012890]

Can I target many color combinations (e.g., blue/yellow, purple/yellow) without rewriting code?

Yes. Use one UDF and change only the reference cell to each color pair you need. The function matches the pattern in that cell, not typed color names. Create separate formulas for each pair for clear reporting. [Elektroda, lanzul, post #17013693]

How many colors can Excel handle, and does that affect matching?

Legacy ColorIndex exposes 56 slots, while RGB-based .Color covers a broad spectrum. The posted macro can list ColorIndex values to help you choose patterns that match your theme. Use one reference cell per target combo to avoid ambiguity. [Elektroda, lanzul, post #17013693]

Will this be slow or error-prone on big ranges?

Large ranges can stress types and loops. The thread suggests Variant/Double for safety when iterating many cells, since Long may overflow in edge cases. Keep ranges tight and avoid unnecessary recalculations to improve speed. [Elektroda, lanzul, post #17011172]

How can I list or preview available color indexes inside Excel?

Run the provided kolorki macro. It fills cells with successive ColorIndex values and labels them. This quick palette lets you pick a consistent background or font color for your reference cell and targets. [Elektroda, lanzul, post #17013693]

Is there a no‑VBA alternative for color-based reports?

If you generate reports, consider Python with XlsxWriter to set colors programmatically during export. You compute totals from data, then color cells to reflect status. This avoids UDF recalculation limits in Excel. [Elektroda, pseudocfoch, post #21313586]

Where can I see a tutorial or doc on color-counting methods?

The thread points to Microsoft’s support article that demonstrates counting cells by color using VBA. It shows the basic pattern and helps you adapt the code for sums or counts. [Elektroda, adamas_nt, post #17008933]

How do I implement the two‑criteria UDF in three steps?

  1. Insert a Module in the VBA editor and paste the two-criteria SumCellsByColor code.
  2. In the sheet, place a sample cell with the desired font and background.
  3. Enter =SumCellsByColor(target_range; reference_cell) and press F9 after recoloring. [Elektroda, adamas_nt, post #17013903]

Why doesn’t my total change when I just recolor a cell?

Manual color changes may not trigger a recalc event for UDFs. The posted solution uses Application.Volatile, but you may still need to press F9 to refresh the result after recoloring. That is expected behavior. [Elektroda, dariuszb09, post #17011090]
Generated by the language model.
ADVERTISEMENT