Table of Contents
While Microsoft Excel is equipped with robust tools for data analysis, performing calculations based on cell formatting, such as summing values contingent upon their background color, poses a unique challenge. Standard functions like SUMIF or SUMIFS are designed to evaluate criteria based on cell content (numbers, text, dates), not visual properties. To overcome this limitation and accurately calculate totals based on color coding, we must utilize the power of VBA (Visual Basic for Applications) to create a custom function.
This specialized approach allows you to define a user-defined function (UDF) that can iterate through a specified range of cells, determine the interior color of each cell, and accumulate the values only if the color matches a specified criterion cell. This method offers unparalleled flexibility when dealing with datasets that rely heavily on conditional formatting or manual color coding for categorization.
Understanding the Challenge: Summing by Cell Interior Color
It is frequently necessary in complex spreadsheets to aggregate numerical values based on their visual appearance. For instance, color might signify priority level, completion status, or category membership. Standard filtering options in Excel, such as the ‘Filter by Color’ feature, allow for visual segmentation but do not provide an immediate summation result that can be referenced in other calculations.
Consider a typical dataset where values are color-coded (e.g., red for urgent, green for complete) representing different statuses. Our objective is to generate a summary table that displays the total sum for all values belonging to each specific color category. Since Excel lacks a native function to perform this operation, it requires a programmatic solution, which is where a Macro developed in VBA becomes essential.
Suppose we have the following data structure in our worksheet, and we intend to sum the numerical entries based on their respective cell background colors:

While this procedure might seem complex for those unfamiliar with Excel’s programming environment, the implementation is straightforward. We will guide you through the process of setting up the VBA code and integrating the custom function into your worksheet formulas efficiently.
Step 1: Preparing Your Dataset in Excel
The initial step requires organizing your data within the Excel sheet. Ensure that the numerical values you wish to sum are clearly defined in a single column or range, and that the corresponding cells have been formatted with the necessary background colors. For this demonstration, we will assume the data is entered into column A, starting from row 2.
Accuracy in data entry and consistent color application are crucial at this stage. Any variation in formatting or inconsistent coloring will lead to inaccurate summation results once the custom function is applied. Double-check that all cells intended for summation are correctly populated and colored before proceeding to the programming steps.
As illustrated previously, input the values into your worksheet. This step involves only standard data entry and formatting:

Step 2: Enabling the Developer Tab for Advanced Tools
To access the VBA editor, which is where we will write our custom function, you must first ensure that the Developer tab is visible on the Excel ribbon. By default, this tab is hidden in most installations, as it contains advanced tools necessary for programming, form controls, and XML functionality.
To activate the Developer tools, navigate through the Excel interface using the following sequence:
- Click the File tab located in the top-left corner of the Excel window.
- Select Options near the bottom of the navigation pane. This opens the Excel Options dialog box.
- In the Options dialog, click Customize Ribbon in the left-hand menu.
- Under the section labeled Main Tabs on the right side, locate and check the box corresponding to Developer.
- Confirm your selection by clicking OK.
Once confirmed, the Developer Tab will now be prominently featured in your main Excel ribbon, providing immediate access to the Visual Basic editor, which is necessary for the next programming step.

Step 3: Accessing the Visual Basic Editor and Creating a Module
With the Developer tools active, we can proceed to write the specific code that will perform the color-based summation. This code will be encapsulated within a custom Function, allowing it to be referenced directly within a standard Excel formula, just like built-in functions such as SUM or AVERAGE.
Begin by launching the Visual Basic Editor (VBE). Click the Developer tab on the ribbon, and then click the Visual Basic icon (located on the far left of the Developer tools group). Alternatively, you can use the keyboard shortcut Alt + F11.

Inside the Visual Basic Editor, you need to insert a new module where the code will reside. Modules are essential container objects for storing general procedures and custom functions. Click the Insert menu at the top of the VBE window, and then select Module from the dropdown list. This action opens a blank code window, ready for input.

Step 4: Inserting and Explaining the VBA Function Code
Now, paste the following VBA code into the newly created module window. This code defines the custom function named SumCellsByColor, which takes two essential arguments: the range of cells containing the values to sum (CellRange), and a reference cell whose background color is used as the matching criterion (CellColor).
The core logic of the function involves retrieving the numerical ColorIndex property of the reference cell. It then iterates through every individual cell in the input range (CellRange). For each cell, it compares its Interior.ColorIndex to the criteria value. If the colors match, the numerical value of that cell is added to the RunningSum variable, effectively accumulating the total for that specific color.
Copy and paste the following code exactly as presented:
Function SumCellsByColor(CellRange As Range, CellColor As Range) Dim CellColorValue As Integer Dim RunningSum As Long CellColorValue = CellColor.Interior.ColorIndex Set i = CellRange For Each i In CellRange If i.Interior.ColorIndex = CellColorValue Then RunningSum = RunningSum + i.Value End If Next i SumCellsByColor = RunningSum End Function
This screenshot confirms the placement of the user-defined function within the module editor in the Visual Basic Editor (VBE):

After the code has been successfully inserted, close the VBE window to return to your worksheet. The custom function is now saved within your workbook and is ready to be utilized in your sheet formulas. It is critical to note that since this involves a Macro, the workbook must be saved as an Excel Macro-Enabled Workbook (.xlsm) to ensure the code persists.
Step 5: Implementing the Custom Function in the Worksheet
The final step involves calling the newly created SumCellsByColor function directly into your Excel sheet to generate the required sums. This function behaves exactly like any native Excel function, requiring only two inputs: the range containing the values and the reference cell defining the desired color criterion.
For clear and organized presentation of results, set up a small summary area adjacent to your data. First, fill cells (for example, C2:C4) with the specific colors you wish to sum. These cells serve as the color criteria input for our custom function.
Next, navigate to the cell where you want the first total to appear (e.g., cell D2, which is next to the first criteria color). Enter the following formula, ensuring you adjust the numerical ranges if your data setup differs from this example:
=SumCellsByColor($A$2:$A$11, C2)
In this formula:
$A$2:$A$11is the absolute reference to the range containing all numerical values (the data range). The absolute reference is necessary so this range does not shift when dragging the formula down.C2is the relative reference to the cell containing the reference color (the criteria cell). This reference must be relative so it correctly adjusts to C3, C4, etc., when filled down.
Step 6: Verifying and Finalizing the Results
After entering the formula into cell D2, you can use the fill handle feature in Excel to populate the remaining totals. Drag the formula down to the remaining cells in column D (e.g., D3 and D4). As designed, the formula will automatically compare the fixed data range against the shifting criteria colors in cells C3 and C4, respectively.
The resulting summary table will instantly display the calculated sum for each color category:

For instance, observe the total calculated for the cells with a light green background, which is displayed as 53. We can manually confirm this result by inspecting the source data: the values colored light green are 20, 13, and 20. The sum of these values is indeed 20 + 13 + 20 = 53. This successful verification confirms that our custom function is working correctly and efficiently calculating sums based on cell interior formatting, thereby solving a critical analytical challenge that standard Excel functions cannot address.
Cite this article
stats writer (2025). How to Sum Cells by Color in Excel: A Step-by-Step Guide. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-sum-by-color-in-excel/
stats writer. "How to Sum Cells by Color in Excel: A Step-by-Step Guide." PSYCHOLOGICAL SCALES, 19 Nov. 2025, https://scales.arabpsychology.com/stats/how-to-sum-by-color-in-excel/.
stats writer. "How to Sum Cells by Color in Excel: A Step-by-Step Guide." PSYCHOLOGICAL SCALES, 2025. https://scales.arabpsychology.com/stats/how-to-sum-by-color-in-excel/.
stats writer (2025) 'How to Sum Cells by Color in Excel: A Step-by-Step Guide', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-sum-by-color-in-excel/.
[1] stats writer, "How to Sum Cells by Color in Excel: A Step-by-Step Guide," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, November, 2025.
stats writer. How to Sum Cells by Color in Excel: A Step-by-Step Guide. PSYCHOLOGICAL SCALES. 2025;vol(issue):pages.
