You cannot use the SUMIF function alone to sum cells by their fill color in Google Sheets, because SUMIF only works with cell values, not formatting. To sum by color, you must either use a custom Google Apps Script function or filter by color and then sum the visible cells.
Why SUMIF Can’t Read Cell Colors
SUMIF and SUMIFS evaluate a range based on criteria like numbers, text, or dates. They do not have access to a cell’s formatting attributes (background color, font color, or conditional formatting rules). The color of a cell is metadata, not data, and the built-in functions in Google Sheets ignore it entirely. This is a common misconception because conditional formatting can visually highlight cells, but the underlying values remain unchanged.
Method 1: Use a Custom Google Apps Script Function
The most flexible way to sum by color is to write a small script that reads the background color of each cell and sums the corresponding values. Here is a step-by-step guide:
- Open your Google Sheet and go to Extensions > Apps Script.
- Delete any existing code and paste the script below.
- Click Save (the disk icon) and name your project (e.g., “SumByColor”).
- Close the Apps Script editor and return to your sheet.
- In any cell, type
=SUMCOLOR(range, colorCell)whererangeis the range to sum andcolorCellis a cell with the fill color you want to match.
function SUMCOLOR(range, colorCell) {
// Get the background color of the reference cell
var sheet = SpreadsheetApp.getActiveSheet();
var color = sheet.getRange(colorCell.getA1Notation()).getBackground();
// Get the range to sum
var rangeValues = range.getValues();
var rangeBackgrounds = range.getBackgrounds();
var sum = 0;
for (var i = 0; i < rangeValues.length; i++) {
for (var j = 0; j < rangeValues[i].length; j++) {
if (rangeBackgrounds[i][j] === color) {
sum += rangeValues[i][j];
}
}
}
return sum;
}
This script compares the background color of each cell in the range to the color of the reference cell. It only sums numeric values. To use it, you must have at least one cell with the target color, or you can hardcode the hex code (e.g., #ff0000) directly in the script.
Method 2: Filter by Color and Use SUBTOTAL
If you prefer not to use scripts, you can manually filter by color and then use the SUBTOTAL function to sum only visible cells. Here's how:
- Select your data range.
- Go to Data > Create a filter.
- Click the filter icon in the column header that contains the colored cells.
- Choose Filter by color > Fill color and select the color you want.
- After filtering, in a cell below or beside the data, enter
=SUBTOTAL(109, A2:A100)(assuming your values are in A2:A100).
The 109 argument tells SUBTOTAL to use the SUM function while ignoring rows hidden by the filter. This method is quick and doesn't require scripting, but it's manual—you must reapply the filter each time the data changes.
How to Choose the Right Method
Both methods work, but they suit different situations:
- Use the Apps Script function if you need a dynamic, automatically updating sum that changes when you change cell colors. It's also better if you need to sum by multiple colors simultaneously (you can copy the function and change the color reference).
- Use SUBTOTAL with filtering if you want a one-off calculation without any code, or if you're not comfortable with scripts. It's also faster for large datasets because it doesn't run a script on every edit.
Common Problems and Troubleshooting
Here are typical issues users encounter:
- Script returns #NAME? This usually means the function name is misspelled or the script hasn't been saved properly. Re-open Apps Script and ensure the function is named exactly
SUMCOLOR(or whatever you called it). - Sum is zero even though there are colored cells. Check if the colors are from conditional formatting. The script reads the actual background color, which conditional formatting sets, so it should work. But if you have manually colored cells, make sure the hex codes match exactly—different shades of red are different colors.
- Script sums text cells as zero. The script adds all values, but non-numeric values are treated as 0 in arithmetic. If you have text in the range, it will be ignored.
- Filter by color option is grayed out. This happens when your data isn't in a proper range or when the filter is applied to a single cell. Ensure you select the entire data range before creating the filter.
Comparing SUMIF, SUMIFS, and Color-Based Summing
SUMIF and SUMIFS are built-in and fast, but they only work with values. Color-based summing is a workaround for when your data is visually categorized but lacks a corresponding value column. For example, you might have a budget sheet where expenses are highlighted in red for 'over budget' and green for 'under budget'. Without a status column, you can't use SUMIF. Color summing fills that gap, but it's less robust because it depends on formatting consistency. If someone changes a fill color, your sum changes silently—which can be a feature or a bug depending on your workflow.
Is There a Built-In Function in 2024?
As of now, Google Sheets does not have a native SUMIF function that reads colors. There are no built-in functions like SUMCOLOR or SUMIFCOLOR. The only official way is via Apps Script or the filter method. Some add-ons like 'Power Tools' offer color-summing features, but they are third-party and may require payment. The Apps Script method is free and permanent—once you add the script, it's available in that spreadsheet.
Frequently Asked Questions
Can I sum by font color instead of fill color?
Yes, you can modify the script to use getFontColors() instead of getBackgrounds(). The logic is identical; just replace the two lines that get backgrounds with font colors. For the filter method, you can choose 'Text color' instead of 'Fill color' in the filter menu.
Will the script work if I use conditional formatting?
Yes, the script reads the actual displayed background color, which includes colors set by conditional formatting rules. However, if you change the conditional formatting rule, the sum will update accordingly because the cell's background color changes.
How do I sum multiple colors at once?
You can either create a separate SUMCOLOR function for each color and add them together, or modify the script to accept an array of colors. For example, =SUMCOLOR(A1:A10, "#ff0000") + SUMCOLOR(A1:A10, "#00ff00"). Alternatively, you can write a more advanced script that loops through a list of colors.
Does this work in Excel?
No, this article is for Google Sheets. Excel also lacks a built-in SUMIF for colors, but you can use VBA with the Cell.Interior.Color property. The approach is similar but requires a different scripting language. In Excel, you can also use the 'Filter by Color' feature and SUBTOTAL, which works the same way.
Is there a way to do this without scripting?
Yes, the filter + SUBTOTAL method is script-free. It's manual but reliable. If you need a dynamic solution without scripts, consider adding a helper column that contains a value representing the color (e.g., 1 for red, 2 for green) and then use SUMIF on that helper column. This is often the best practice because it makes your data more structured.
Why does my script return an error when I use it on a different sheet?
The script is bound to the spreadsheet where you created it. If you copy the script to another spreadsheet, it will work there. But if you try to reference a range from a different spreadsheet, you'll need to adjust the script to use SpreadsheetApp.openById() to get the external sheet. For most users, keeping the script in the same spreadsheet is simplest.
Related
- Free Employee Schedule Template Google Sheets
- How to Generate Random Numbers in Excel
- Alphalist Data Entry Version 6.1 Explained
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Unmerge Cells in Excel: Shortcut Key and Steps for WindowsIn Excel for Microsoft 365 on Windows, select a merged cell and press Alt, H, M, then U—one…
- Shortcuts in a Computer? Explained SimplyShortcuts in a computer are time-saving commands or icons that let you perform an action with fewer steps…
- Track Changes in Word: How to Review and Accept EditsTo use Track Changes in Microsoft Word, open the Review tab and select Track Changes; Word will mark…
- Google Sheets: How to Alphabetize RowsTo alphabetize rows in Google Sheets, select the whole table, open Data > Sort range > Advanced range…
- How to Track Changes in Microsoft Word and Review EditsTo track changes in Microsoft Word, open the document, select Review > Track Changes, then edit as usual.…
- WEEKDAY in Google Sheets: Formula, Numbering, and TipsIn Google Sheets, use =WEEKDAY(A2,2) to return the weekday number for a date in cell A2, with Monday…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.