How to Average by Color in Excel (Step-by-Step Example)


Often you may want to average values in Excel based on their color.

For example, suppose we have the following dataset and we’d like to calculate the average value of cells based on the cell colors:

The easiest way to do this is by writing some code in VBA in Excel.

This might seem intimidating if you’re not familiar with VBA but the process is straightforward and the following step-by-step example shows exactly how to do so.

Step 1: Enter the Data

First, enter the data values into Excel:

Step 2: Show the Developer Tab in Excel

Next, we need to make sure the Developer tab is visible on the top ribbon in Excel.

To do so, click the File tab, then click Options, then click Customize Ribbon.

Under the section called Main Tabs, check the box next to Developer, then click OK:

Step 3: Create a Macro Using VBA

Next, click the Developer tab along the top ribbon and then click the Visual Basic icon:

Next, click the Insert tab and then click Module from the dropdown menu:

Next, paste the following code into the module code editor:

Function AvgCellsByColor(CellRange As Range, CellColor As Range)

Dim CellColorValue As Integer
Dim RunningAvg As Long
Dim RunningSum As Long
Dim RunngingCount As Long

CellColorValue = CellColor.Interior.ColorIndex
Set i = CellRange

For Each i In CellRange
    If i.Interior.ColorIndex = CellColorValue Then
    RunningSum = RunningSum + i.Value
    RunningCount = RunningCount + 1
    End If
Next i

AvgCellsByColor = RunningSum / RunningCount

End Function

The following screenshot shows how to do so:

Next, close the VB Editor.

Step 4: Use the Macro to Average by Color

Lastly, we can use the macro we created to calculate the average of cell values based on color.

First, fill in cells C2:C4 with the colors that you’d like to calculate the average value for.

Then type the following formula into cell D2:

=AvgCellsByColor($A$2:$A$11, C2)

Drag and fill this formula down to each remaining cell in column D and the formula will automatically calculate the average value for each of the cells that have specific background colors:

Excel average by color

For example, we can see that the average value of the cells with a light green background is 17.67.

We can confirm this by manually calculating the average of each cell with a light green background:

Average of Cells with Light Green Background: (20 + 13 + 20) / 3 = 17.67.

This matches the value calculated by our formula.

Additional Resources

The following tutorials explain how to perform other common operations in Excel:

How to Calculate Average by Group in Excel
How to Calculate Average by Month in Excel
How to Calculate Average by Day of Week in Excel

4 Replies to “How to Average by Color in Excel (Step-by-Step Example)”

  1. Hello. I am attempting to create the macro using the VBA code to create an average by color formula.
    https://www.statology.org/excel-average-by-color/

    The number I expect to receive (and the numbers to be calculated are not integers, though).

    I attempted to change the “Dim CellColorValue As Integer” to “Dim CellColorValue As Variant”, but had no success in having the average appear. Would you mind assisting with any other command to include, so that decimal numbers can be included in the calculation? Thank you so much! I would not have even been close to obtaining results without your Statology site.

    1. Hi John…Thank you for your feedback! Please provide more detail around any error messages or unexpected results from your implementation so that we may better guide you.

  2. Hello,
    I am trying to create a color ‘score’ and wondering if there is a way to create an ‘average’ color based on a group of colored cells.

    I am not able to paste a screenshot of what I mean here.
    If you reply, perhaps I can share more.

    Thanks

    1. Hi Dale…Would it possible to try to put together a visual and screenshot again? That will help us to better understand the use case and provide guidance.

Leave a Reply

Your email address will not be published. Required fields are marked *