Understanding How to Count Colored Cells in Google Sheets
Many Google Sheets users find themselves needing to count cells based on their color. This is a common task, whether you're tracking inventory with different colored tags, categorizing survey responses, or simply organizing data for better visual appeal. While Google Sheets doesn't have a direct "COUNTCOLOR" function like some other spreadsheet programs, there are several effective methods you can use to achieve this. This article will guide you through the most practical approaches, explaining each step in detail.
Method 1: Using the `FILTER` Function and `COUNTA`
This is often the most straightforward and dynamic method for counting colored cells, especially when the coloring is based on conditional formatting rules. It leverages the power of the `FILTER` function to isolate the cells you want and then uses `COUNTA` to count them.
Steps:
- Identify the Color and its Condition: First, determine the specific color you want to count and, crucially, the condition that applied that color. For example, if you have cells colored red when a value is less than 10, you need to know that "less than 10" is the condition.
- Access the `FILTER` Function: In an empty cell where you want the count to appear, type the following formula: `=FILTER(range, condition)`.
- Specify the Range: Replace `range` with the actual range of cells you want to examine. For instance, if your data is in cells A1 through A100, you would use `A1:A100`.
-
Define the Condition: This is where you replicate the rule that created the color. If cells are colored red when the value is less than 10, your condition would be `range < 10`. So, if your range is A1:A100, the condition would be `A1:A100 < 10`.
Example: If you want to count all cells in the range `B2:B50` that are colored red because their value is greater than 50, your formula would be:
=FILTER(B2:B50, B2:B50 > 50) -
Count the Filtered Results: Now, wrap the `FILTER` function within the `COUNTA` function to count the number of cells returned by the filter. The `COUNTA` function counts all non-empty cells.
Formula:
=COUNTA(FILTER(range, condition))
Example: To count the red cells (value > 50) in `B2:B50`:
=COUNTA(FILTER(B2:B50, B2:B50 > 50))
Important Note: This method works best when the cell colors are applied through conditional formatting. If cells are manually colored, this method will not work directly.
Method 2: Using a Custom Function with Google Apps Script
For situations where cells are colored manually, or you need a more robust solution, you can create a custom function using Google Apps Script. This requires a bit more technical setup but offers greater flexibility.
Steps:
- Open the Script Editor: In your Google Sheet, go to Extensions > Apps Script.
-
Write the Custom Function: In the script editor, delete any existing code and paste the following script. This function `COUNTBYCOLOR` takes a range and a color reference (either a cell with the desired color or a specific RGB hex code) as input.
function COUNTBYCOLOR(range, color) { var colorHex = ''; if (typeof color === 'object') { colorHex = color.getFontColor(); // If it's a cell, get its font color if (colorHex === '#000000') { // If font color is black, try background color colorHex = color.getBackgroundColor(); } } else { colorHex = color; // Assume it's a hex code if not an object } var count = 0; var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var dataRange = sheet.getRange(range); var colors = dataRange.getBackgrounds(); // Get background colors of the range for (var i = 0; i < colors.length; i++) { for (var j = 0; j < colors[i].length; j++) { if (colors[i][j].toLowerCase() === colorHex.toLowerCase()) { count++; } } } return count; } - Save the Script: Click the floppy disk icon to save your script. Give it a descriptive name, like "Custom Functions".
-
Use the Custom Function in Your Sheet: Close the script editor and return to your Google Sheet. You can now use your custom function in any cell.
Syntax:
=COUNTBYCOLOR(range, color_reference)
Examples:-
Counting cells with a specific background color (e.g., light yellow): If cells A1:A10 have a light yellow background, and cell C1 has the same light yellow background color, you can use:
=COUNTBYCOLOR("A1:A10", C1) -
Counting cells with a specific background color using a hex code: If you know the hex code for your desired color (e.g., "#ffe599" for light yellow), you can use:
=COUNTBYCOLOR("A1:A10", "#ffe599") -
Counting cells with a specific font color: The script also checks font color if the background color is black. To count cells with red font color in range D1:D20, assuming cell E1 has red font:
=COUNTBYCOLOR("D1:D20", E1)
-
Counting cells with a specific background color (e.g., light yellow): If cells A1:A10 have a light yellow background, and cell C1 has the same light yellow background color, you can use:
Customization for Font Color:
The provided script prioritizes background color. If you primarily need to count based on font color, you can modify the script. For instance, if you want to count cells with a red font color in range `A1:A10`, and cell `B1` has a red font:
- Open your Apps Script editor.
-
Replace the existing `COUNTBYCOLOR` function with this modified version:
function COUNTBYFONTCOLOR(range, color) { var colorHex = ''; if (typeof color === 'object') { colorHex = color.getFontColor(); // Get the font color of the reference cell } else { colorHex = color; // Assume it's a hex code if not an object } var count = 0; var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var dataRange = sheet.getRange(range); var fontColors = dataRange.getFontColors(); // Get font colors of the range for (var i = 0; i < fontColors.length; i++) { for (var j = 0; j < fontColors[i].length; j++) { if (fontColors[i][j].toLowerCase() === colorHex.toLowerCase()) { count++; } } } return count; } - Save the script.
- In your sheet, use the function like this: `=COUNTBYFONTCOLOR("A1:A10", B1)` or `=COUNTBYFONTCOLOR("A1:A10", "#FF0000")` (for red).
Method 3: Using `QUERY` with Conditional Formatting
Similar to the `FILTER` method, the `QUERY` function can be used to count cells when the coloring is based on conditional formatting. This method is powerful and can handle more complex conditions.
Steps:
- Understand your Conditional Formatting Rule: As with the `FILTER` method, you need to know the exact condition that triggers the color.
- Construct the `QUERY` Formula: The `QUERY` function uses a SQL-like syntax. The basic structure for counting is: `=QUERY(range, "SELECT COUNT(ColX) WHERE ColX condition")`.
- Specify the Range and Column: Replace `range` with your data range. To refer to a column within that range, use `Col1`, `Col2`, etc., where `Col1` refers to the first column in your specified range, `Col2` to the second, and so on.
-
Define the Condition: This is where you translate your conditional formatting rule into the `QUERY` language.
Example: If you have data in `C1:C50` and cells are colored green when the value is greater than 100, you would use `Col1` to refer to the `C` column. The formula would be:
=QUERY(C1:C50, "SELECT COUNT(Col1) WHERE Col1 > 100")
Note: The `QUERY` function, like `FILTER`, works best with conditional formatting. It doesn't directly interpret cell colors that were applied manually.
Frequently Asked Questions (FAQ)
How do I count cells based on manual coloring?
If you have manually colored cells (not through conditional formatting), you'll need to use a custom function created with Google Apps Script. The `COUNTBYCOLOR` script provided in Method 2 can handle this by allowing you to reference a cell with the desired color or input the color's hex code.
Why doesn't Google Sheets have a built-in COUNTCOLOR function?
Google Sheets prioritizes dynamic data analysis. Conditional formatting is designed to automatically update based on data values. Therefore, functions that interact with conditional formatting rules (like `FILTER` and `QUERY`) are more integrated into its core functionality. Manual cell coloring is less about data logic and more about visual presentation, making it a task better suited for custom scripting.
Can I count cells based on both background color and font color simultaneously?
The provided `COUNTBYCOLOR` script primarily focuses on background color but includes a fallback for font color if the background is black. To count based on both simultaneously, you would need to modify the script further to check both properties and only increment the count if both criteria are met for a given cell. This would involve more advanced scripting.
What's the difference between counting based on conditional formatting and manual coloring?
Counting based on conditional formatting is dynamic. If the data changes and a cell's color changes due to a rule, formulas like `FILTER` and `QUERY` will automatically update the count. Counting based on manual coloring is static. The count will only reflect the colors present at the moment the script is run, and it won't update automatically if colors are changed manually.
Can I count cells that have been colored using a color scale in conditional formatting?
Counting cells within specific color ranges from a color scale can be tricky with standard functions. You would typically need to break down the color scale into distinct ranges and apply conditional formatting rules for each range, then count those separately, or use a custom script that can interpret the color scale logic.

