Ever found yourself poring over Google Sheets, wondering if there's a quick way to count cells based on their color? While Google Sheets doesn't have a built-in function for this, there are indeed workarounds to achieve this. Let's delve into the world of Google Sheets' conditional formatting and scripts to explore how to count colored cells.

Before we dive in, it's crucial to understand that Google Sheets doesn't natively support counting cells based on their fill color. However, we can use a combination of conditional formatting, data validation, and even Google Apps Script to accomplish this task. So, let's get started!

Using Conditional Formatting and Data Validation
This method involves using conditional formatting to mark cells based on their color and then using data validation to count these marked cells. Here's how you can do it:

First, apply conditional formatting to mark cells based on their color. Select the range of cells, click on 'Format' in the menu, then 'Conditional formatting'. Under the 'Format cells if...' dropdown, choose 'Custom formula is', and enter the formula to match the color you're interested in. For example, to match red cells, you might use '=A1<>"".
Marking Cells Based on Color
![Google Sheets - Sum or Count Values Based on Cell Color [Improved]](https://i.pinimg.com/originals/67/09/17/670917e64eae67d866bb380430170904.jpg)
In the formula, 'A1<>""' checks if the cell is not empty. You'll need to adjust this based on the color you're targeting. Once you've set the formula, choose the formatting style (like a fill color or a border), and click 'Done'.
Next, use data validation to count these marked cells. Select the range of cells, click on 'Data' in the menu, then 'Data validation'. Under 'Criteria', choose 'Custom formula is', and enter the same formula you used for conditional formatting. Click 'Save'.
Counting Marked Cells

Now, when you select the range of cells, Google Sheets will display the count of cells that match the criteria in the top-left corner. This count updates automatically as you add or remove cells that match the criteria.
Using Google Apps Script
If you're comfortable with a bit of coding, you can use Google Apps Script to create a custom function that counts cells based on their color. Here's a simple script to get you started:

In your Google Sheets document, click on 'Extensions' in the menu, then 'Apps Script'. Delete any code in the 'Code.gs' file, and paste the following:
```javascript function COUNTCOLOREDCELLS(range, color) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getActiveSheet(); var cells = sheet.getRange(range).getCells(); var count = 0; for (var i = 0; i < cells.length; i++) { if (cells[i].getBackground() == color) { count++; } } return count; } ```
Creating the Custom Function


















This script defines a function called 'COUNTCOLOREDCELLS' that takes a range and a color as arguments. It loops through each cell in the range, checks its background color, and increments a counter if the color matches.
To use this function, select the range of cells, then click on the cell where you want the count to display. Type '=COUNTCOLOREDCELLS(', then select the range of cells, a comma, the color in hexadecimal format (like '#FF0000' for red), and a closing parenthesis. Press Enter, and the function will display the count of cells with the specified color in the range.
In conclusion, while Google Sheets doesn't have a built-in way to count cells based on their color, a combination of conditional formatting, data validation, and Google Apps Script can help you achieve this. Whether you're marking and counting cells based on their color for data analysis or just for fun, these methods can save you time and effort in your Google Sheets workflow. Happy counting!