Calculate min, max and average of a range of cells, but only of coloured cells

Danikuku

New Member
Joined
Aug 27, 2014
Messages
16
Office Version
  1. 365
Hi Guys,

I need to calculate min, max and average of a range of cells (ex. A2:C6).
This appears to be easy, except in this range of cells, I have some cells with different background colours and what I want is to calculate this min, max and average of all cells that have a background colour (independently of the colour).

In another words, I want the formula to ignore all cells with no background colour and consider only the coloured ones.

Thanks!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi,

I have assumed that these are fill colours and NOT conditional formatting. ALT+F11 to open VB editor, right click ThisWorkbook and insert module and paste the code below in on the right. Close vb editor.

Back on the worksheet call with this in a cell


=CountColour(A1:C6,n)

Where n =

1 for minimum
2 for maximum
3 for average




Code:
Function CountColour(rng As Range, TheType As Long) As Double
Dim MyRange As Range
Application.Volatile
Dim c As Range
For Each c In rng
    If c.Interior.Color <> 16777215 Then
       If MyRange Is Nothing Then
                Set MyRange = c
        Else
                Set MyRange = Union(MyRange, c)
        End If
    End If
Next
Select Case TheType
    Case Is = 1
        CountColour = WorksheetFunction.Min(MyRange)
    Case Is = 2
        CountColour = WorksheetFunction.Max(MyRange)
    Case Is = 3
        CountColour = WorksheetFunction.Average(MyRange)
    End Select
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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