Quantcast
Channel: Functions | Ok Sheets
Viewing all articles
Browse latest Browse all 10

3 Ways to Sum Colored Cells in Google Sheets

$
0
0

Looking for an easy method to sum colored cells in Google Sheets? You’re in luck! 

Google Sheets offers three powerful ways to sum colored cells in a spreadsheet. You can sum them using the SUBTOTAL function, app script function, and even add-ons. 

Hop on to the article below to learn all about these functions and streamline your workflow in no time. So without further ado, let’s jump right into it. 

How to Sum Colored Cells Using the SUBTOTAL Function 

The SUBTOTAL function is a complete game changer when coupled with Filter to sum colored cells. Unlike other Google Sheets functions, SUBTOTAL performs operations on only those cells that are visible on the screen. 

This means if there are any cells that are hidden or filtered, the SUBTOTAL function will exclude them from the calculation. That’s what does the trick for us here. 

Let’s see a detailed demo of this feature below. 

The SUBTOTAL function’s syntax is given as: 

=SUBTOTAL(function_num, range1, [range2], ...)
  • function_num is the function to be used which lies between 1-11, i.e., SUM is 9. If you want to exclude the hidden cells from the calculation, then add 100 to each number so that it lies between 101-111, i.e., 109 for SUM excluding filtered cells. 
  • range1 is the range upon which the calculation is to be performed.
  • range2 is similar to range 1 but is an optional argument.

Now, say you have this example data. You want to sum up all the colored cells in this data set. 

=SUBTOTAL(109, D2:D11)

Enter the above SUBTOTAL function. 

The first parameter (109) specifies the SUM function in the formula. The second parameter specifies the range (D2:D11) upon which the function is to be applied. 

Now, comes the interesting part. 

Go to the Data tab and select Create a filter from the dropdown menu. 

A small filter icon will appear next to each column. Click the filter icon and a drop-down menu will appear. 

  1. Go to Filter by Color
  2. Choose the Fill Color option. 
  3. Select a color from the options.

All cells except for your selected color will be removed and the SUBTOTAL function will show the SUM of the colored cells as a result.  

Isn’t this cool? Try it yourself.  

How to Sum Colored Cells Using Add Ons

Now see how to use Google Sheets Add On to sum colored cells. This method is slightly longer but once you get a hold of it, you will be able to do it in seconds. 

First off, we need to enable Add Ons and add Ablebits Power Tools

To do that, 

  1. Go to the Extensions tab. 
  2. Click Add-ons from the dropdown
  3. Select Get Add-ons from the options. 
  1. The Google Workspace Marketplace prompt will appear on the screen. 
  1. Type in Power Tools in the Search bar and select the first option that appears. 
  1. Install Power Tools. it won’t take more than a minute. 
  1. Click Continue on the prompt to install. 
  1. After installation, the Power Tools dialog box will appear on the right side of your screen. 
  1. If it doesn’t appear automatically, go to the Extensions tab. 
  2. Select Power Tools
  3. Click Start
  1. On the Power Tools menu bar, select the Sigma option (Σ). 
  2. Select Function by Color from the options. 
  1. Select the SUM function from the Use function list. 
  1. Enter the range containing colored cells you want to sum and select color of the cells.
  2. Click the Insert function button. 

Google Sheets will sum the colored cells in your selected range in seconds and return the result. 

Isn’t this fun? It will take you some time to understand at first but all you need is a little practice to master the art of summing colored cells. 

How to Sum Colored Cells Using the App Script Function 

You can also use the App Script function to sum colored cells in your data set. 

Note that App Scripts requires some basic knowledge of Java Script as the code is based on it. But you should be able to work it fine even if you are not well-versed with Java Script.

You can access the Scripts Editor by going to the Tools menu and selecting the Script Editor option. 

If this option doesn’t work, go to Extensions > App Scripts. This will take you to the main page where you can add your code and save it easily. 

/**
* This function will return the count of cells in the given range of cells based on a given color
* @param1 range - This is the range of cells to count.
* @param2 cell - This is a cell reference with the color to count.
* @returns the count of coloured cells 
* @customfunction
**/
function COUNTCOLOR(range, cell) {
  //Get the formula from the current cell
  var currRange = SpreadsheetApp.getActiveRange();
  var currSheet = currRange.getSheet();
  var currCellFormula = currRange.getFormula();

  //Parse the first parameter and convert to a range reference
  var para1 = currCellFormula.match(/\((.*)\,/).pop();
  var countRange = currSheet.getRange(para1);
  var countRangeBG = countRange.getBackgrounds();

  //Parse the second parameter and convert to a range reference
  var para2 = currCellFormula.match(/\,(.*)\)/).pop();
  var goalCell = currSheet.getRange(para2);
  var goalCellBG = goalCell.getBackground();

  var counter = 0;

  for (var i = 0; i < countRangeBG.length; i++) {
    for (var j = 0; j < countRangeBG[0].length; j++) {
      if (countRangeBG[i][j] === goalCellBG) {
        counter++;
      }
    }
  }
  return counter;
}; 

Simply copy the above-written code into the App Scripts editor and press save. 

But before that, here’s a little insight into the working of this code. 

It is a custom function written in Google Apps Script. It takes two arguments, range and cell which represent the range of cells upon which the function is to be applied and the cell reference containing desired color, respectively. 

The function then parses the arguments and uses it to obtain the range and cell color. In the next step, a variable (SUM) is initialized and assigned a value zero (0). 

The function then loops through all the cells in the range to check if the cells match the background color of the cell argument.

As the colors match, the function adds the cell value to the SUM variable. Once the range ends, the function returns the SUM of values matching the background color of the cell argument. 

As a result, we obtain the SUM of colored cells in our dataset through the SUMCOLOR function. Adding the code allows us to use the SUMCOLOR function in Google Sheets. Its syntax is given as: 

=SUMCOLOR(range, cell)

Where 

  • range specifies the range upon which the calculation is to be performed. 
  • cell is the argument that specifies the color in which you want to sum cells. 

Now enter the arguments for the function in the spreadsheet. 

=SUMCOLOR(D2:D11, A10)

The formula will return the SUM of colored cells in your dataset. 

The first parameter is the range that is to be summed. The second parameter is the cell reference containing the background color which we want to sum. 

Isn’t this an interesting method? Practice it now! 

Conclusions

Knowing how to sum colored cells in Google Sheets can be extremely helpful in quick analysis of data. Highlighting cells not only makes your spreadsheet stand out but also makes data more readable and engaging. 

In this article, we learned how to sum colored cells using the SUBTOTAL function, app scripts, and add-ons and saw how easy the solutions are. 

A little practice is all you need to master these methods. So, what are you waiting for? Use the above methods now to sum colored cells in your data set.


Viewing all articles
Browse latest Browse all 10

Trending Articles