You are viewing the article How to count or sum cells based on cell color in Google sheet? at Lassho.edu.vn you can quickly access the necessary information in the table of contents of the article below.
How to count or sum cells based on cell color in Google sheet?
Counting or summing cell values based on specific cell background color to get the result as following screenshot shown. This article, I will talk about how to solve this task in Google sheet and Microsoft Excel.
Count cell values based on cell color with script in Google sheet
Sum cell values based on cell color with script in Google sheet
Count or sum cell values on cell color with Kutools for Excel in Microsoft Excel
Count cell values based on cell color with script in Google sheet
The following script can help you to count the cell values based on specific cell color, please do as this:
1. Click Tools > Script editor, see screenshot:
2. In the opened project window, click File > New > Script file to open a code window, see screenshot:
3. And in the prompt box, please enter a name for this script code, see screenshot:
4. Click OK and then copy and paste the following code to replace the original code into the code module, see screenshot:
function countColoredCells(countRange,colorRef) { var activeRg = SpreadsheetApp.getActiveRange(); var activeSht = SpreadsheetApp.getActiveSheet(); var activeformula = activeRg.getFormula(); var countRangeAddress = activeformula.match(/((.*),/).pop().trim(); var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds(); var colorRefAddress = activeformula.match(/,(.*))/).pop().trim(); var BackGround = activeSht.getRange(colorRefAddress).getBackground(); var countCells = 0; for (var i = 0; i < backGrounds.length; i++) for (var k = 0; k < backGrounds[i].length; k++) if ( backGrounds[i][k] == BackGround ) countCells = countCells + 1; return countCells; };
5. Then save this script code, and go back the sheet, enter this formula: =countcoloredcells(A1:E11,A1) into a blank cell, and then press Enter key to get the calculated result. See screenshot:
Note: In this formula: A1:E11 is the data range that you want to use, A1 is the cell filled with specific color that you want to count.
6. Repeat the above formula to count other specific colored cells.
Sum cell values based on cell color with script in Google sheet
To sum the cell values with a specific cell color, please apply below script code.
1. Click Tools > Script editor to go the project window, and click File > New > Script file to insert another new code module, then, in the prompt box, please type a name for this script, see screenshot:
2. Click OK and in the opened code module, copy and paste below script code to replace the original code, see screenshot:
function sumColoredCells(sumRange,colorRef) { var activeRg = SpreadsheetApp.getActiveRange(); var activeSht = SpreadsheetApp.getActiveSheet(); var activeformula = activeRg.getFormula(); var countRangeAddress = activeformula.match(/((.*),/).pop().trim(); var backGrounds = activeSht.getRange(countRangeAddress).getBackgrounds(); var sumValues = activeSht.getRange(countRangeAddress).getValues(); var colorRefAddress = activeformula.match(/,(.*))/).pop().trim(); var BackGround = activeSht.getRange(colorRefAddress).getBackground(); var totalValue = 0; for (var i = 0; i < backGrounds.length; i++) for (var k = 0; k < backGrounds[i].length; k++) if ( backGrounds[i][k] == BackGround ) if ((typeof sumValues[i][k]) == 'number') totalValue = totalValue + (sumValues[i][k]); return totalValue; };
3. And then save this code, return to the sheet, and enter this formula: =sumcoloredcells(A1:E11,A1) into a blank cell, and press Enter key to get the calculated result, see screenshot:
Note: In this formula: A1:E11 is the data range that you want to use, A1 is the cell with a specific background color that you want to sum.
4. And then you can repeat the above formula to sum other specific colored cells.
Count or sum cell values on cell color with Kutools for Excel in Microsoft Excel
In Microsoft Excel, to count or sum the cell values based on specific cell color, Kutools for Excel’s Count by Color utility can help you to finish this task as quickly as you can.
Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. |
After installing Kutools for Excel, please do as this:
1. Select the cells to range that you want to count or sum based on cell color, and then click Kutools Plus > Count by Color, see screenshot:
2. In the Count by Color dialog box, choose Standard formatting from the Color method drop down list, and then select Background from the Count type drop down, see screenshot:
3. Then click Generate report button, and new worksheet with the calculated results is generated at once, see screenshot:
Note: With this powerful feature, you can also calculate the cell values based on conditional formatting or font color.
Click Download and free trial Kutools for Excel Now!
The Best Office Productivity Tools
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
- Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails…
- Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range…
- Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns… Prevent Duplicate Cells; Compare Ranges…
- Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select…
- Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more…
- Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments…
- Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic…
- Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF…
- More than 300 powerful features. Supports Office / Excel 2007-2021 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Thank you for reading this post How to count or sum cells based on cell color in Google sheet? at Lassho.edu.vn You can comment, see more related articles below and hope to help you with interesting information.
Related Search: