Formulas Based on Cell Color - SUMIFS, IF, COUNTIF, Etc.

Add to Favorites
Author: | Edits: don

How to use a SUMIF or SUMIFS function (or any conditional function or formula) on cell background color.

There is no default way to do this in Excel, which means that we have to go through a rather annoying multi-step process. That said, there are many ways that you can achieve this result; however I will show you the method that allows you the most versatility and will work when building large SUMIFS functions or any other kind of conditional function such as COUNTIFS or IF.

Section:

Use Conditional Functions on Cell Background Color

Notes

Use Conditional Functions on Cell Background Color

This does require a macro, but it's not difficult to use and I'll walk you through every step.

  1. Add a new column to your data set. This column will store the values that we use to perform the SUMIF or COUNTIF or IF. Make sure the new column is directly to the right of the column with the background colors.
    f95e1ba946b84331c8bd6a34cdcf3ccc.png
  2. Now, we add a small macro.
    Hit Alt + F11 to go to the VBA Window and then go to InsertModule.
    6e1df5272406234aeee2b5876cb85781.jpg
  3. In the window that opens, paste this macro:
    Sub ListColors()
    
    For Each c In Selection 
    
        c.Offset(0, 1).Value = c.Interior.Color 
    
    Next c 
    
    End Sub

    4cf8579e922d50fe2b672e5ab67441a9.png
    Once you've done that, hit AltF11 to go back to the spreadsheet.
  4. Select the cells with the background color.
    260f34b209368b817cd84497ee037d5e.png
  5. Hit Alt + F8 and select the macro that we just added and click the Run button.
    ff4aa9173f463d7b901928b171280b34.png
  6. Now, you will have a column of numbers that represents each color.
    0c4ac7e4b0293ad6895bd65603576342.png
  7. Once you have these values for the numbers, you can then use them in any conditional function or formula.
    You simply refer to the Color Value column when making conditions that limit by color.

Example Formula

Let's SUM all values that start with "gsc" but that are not light green.

=SUMIFS(E2:E9,A2:A9,"gsc*",C2:C9,"<>8122747")

8543be41d5d6a68902352dff1baf942c.png

Important: this macro will put the Color Value numbers in the column that is directly to the right of the column that you selected that has the background colors - if you don't make a new column for it, it will overwrite whatever data is currently there.

Notes

There are many ways to do this kind of thing in Excel, and you can even make custom functions that SUM or COUNT based on color, but I find it more useful to have a separate column for Color Values that you can then use any way you like in any kind of formula or function.

If you don't like the Color Value column or it messes-up the look of your data table, simply hide it after you finished making conditions based on it.

Download the sample file for this tutorial so you can work along.


Excel Function: IF(), SUMIF(), SUMIFS()
Downloadable Files: Excel File

Question? Ask it in our Excel Forum


Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons 50+ Hours of Instruction 200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Tutorial Details
Excel Function: IF(), SUMIF(), SUMIFS()
Downloadable Files: Excel File
Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course