How to average the sum of cells depending on the colour?

KurtisT6

New Member
Joined
Aug 21, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
1661099900181.png
Hello i am looking to find a vba code. I want this code, for example in B13 to find the average of the sum home wins (B7 + B9). I tried to make a vba but I wasnt sure how to involve the average into it.

This was my VBA:

Function SumColor(MatchColor As Range, sumRange As Range) As Double

Dim cell As Range
Dim myColor As Long
myColor = MatchColor.Cells(1, 1).Interior.Color
For Each cell In sumRange
If cell.Interior.Color = myColor Then
SumColor = (SumColor + cell.Value)
End If


Next cell
End Function


ANY HELP WOULD BE APPRECIATED
 

Attachments

  • 1661099835722.png
    1661099835722.png
    9.3 KB · Views: 2

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Why not just count a number each time cell.Interior.Color = myColor, Once you get the final SumColor after completing the loop, just divide the SumColor with total count like
VBA Code:
Function SumColor(MatchColor As Range, sumRange As Range) As Double

Dim cell As Range
Dim myColor As Long, n As Long
n = 0
myColor = MatchColor.Cells(1, 1).Interior.Color
For Each cell In sumRange
If cell.Interior.Color = myColor Then
SumColor = (SumColor + cell.Value)
n = n + 1
End If
SumColor = SumColor / n

Next cell
End Function
 
Upvote 0
hello, thanks for ur reply, when i use this code it comes up with "#value!"
 
Upvote 0
hello, thanks for ur reply, when i use this code it comes up with "#value!"
So sorry. I put SumColor = SumColor / n inside your loop. Should be after all summed up 🤭

Here it is
VBA Code:
Function SumColor(MatchColor As Range, sumRange As Range) As Double

Dim cell As Range
Dim myColor As Long, n As Long
n = 0
myColor = MatchColor.Interior.Color
For Each cell In sumRange
    If cell.Interior.Color = myColor Then
        SumColor = (SumColor + cell.Value)
        n = n + 1
    End If
Next cell
SumColor = SumColor / n

End Function
 
Upvote 0

Forum statistics

Threads
1,215,135
Messages
6,123,239
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top