fselliott
asked on
Find average of cell that are yellow in Excel spreadsheet range
How do I write a formula that will find the average of cells on a spreadsheet that are in column B and the cells are colored yellow?
The yellow cells are not conditionally formatted as they are not based on a condition, they are randomly chosen.
The yellow cells are not conditionally formatted as they are not based on a condition, they are randomly chosen.
You'll need code - is that OK for you?
Or a helper column, if you can't use code.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To add VBA code to a regular or general module in an Excel workbook, press ALT+F11 to open the VBA development environment (VBE). Select the menu command Insert->Module to create a new VBA module. Paste the code into the document window that appears. Press ALT+F11 to return to the Excel workbook.
Kevin
Kevin
The color I used is yellow. Here are some other codes for other shades of "yellow":
Orange = 46
LightOrange = 45
Gold = 44
Tan = 40
DarkYellow = 12
LightYellow = 36
Kevin
Orange = 46
LightOrange = 45
Gold = 44
Tan = 40
DarkYellow = 12
LightYellow = 36
Kevin
Effective.
Here's a more generic approach that you may find useful with less code tailoring in the future.
A link to such is here: http://www.excelexchange.com/SumByColor.html
use the function in that tip to sum or count a range of cells, by a given color....
Here's the code from that tip:
Now, with this pasted in a code module, you can use these two functions:
Function SumByColor(CellColor As Range, SumRange As Range)
and
Function CountByColor(CellColor As Range, CountRange As Range)
To achieve your average, you can put this in your formula:
=SumByColor(B1,B2:B100)/Co unt(B1:B10 0)
Note in this example, cell B1 is a cell you've colored so that you don't have to worry about color code numbers. There are other implementations or even a modification of this one that take an actual color code. I thought it would be nice to share a more generic and code independent version for diversity of answer.
FWIW, the attached file is the posted tip's example spreadsheet...
Cheers,
Dave
SumByColour.xls
Here's a more generic approach that you may find useful with less code tailoring in the future.
A link to such is here: http://www.excelexchange.com/SumByColor.html
use the function in that tip to sum or count a range of cells, by a given color....
Here's the code from that tip:
Function SumByColor(CellColor As Range, SumRange As Range)
Application.Volatile
Dim ICol As Integer
Dim TCell As Range
ICol = CellColor.Interior.ColorIndex
For Each TCell In SumRange
If ICol = TCell.Interior.ColorIndex Then
SumByColor = SumByColor + TCell.Value
End If
Next TCell
End Function
Function CountByColor(CellColor As Range, CountRange As Range)
Application.Volatile
Dim ICol As Integer
Dim TCell As Range
ICol = CellColor.Interior.ColorIndex
For Each TCell In CountRange
If ICol = TCell.Interior.ColorIndex Then
CountByColor = CountByColor + 1
End If
Next TCell
End Function
Now, with this pasted in a code module, you can use these two functions:
Function SumByColor(CellColor As Range, SumRange As Range)
and
Function CountByColor(CellColor As Range, CountRange As Range)
To achieve your average, you can put this in your formula:
=SumByColor(B1,B2:B100)/Co
Note in this example, cell B1 is a cell you've colored so that you don't have to worry about color code numbers. There are other implementations or even a modification of this one that take an actual color code. I thought it would be nice to share a more generic and code independent version for diversity of answer.
FWIW, the attached file is the posted tip's example spreadsheet...
Cheers,
Dave
SumByColour.xls
Also, one could be a bit more graceful with the average. if count(B1:B100) = 0, then you'll get the #DIV!0 result.
You could also code as follows:
=if(COUNT(B2:B100)=0,0,Sum ByColor(B1 ,B2:B100))
or just the original post - syntax corrected to:
=SumByColor(B1,B2:B100)/CO UNT(B2:B10 0)
Dave
You could also code as follows:
=if(COUNT(B2:B100)=0,0,Sum
or just the original post - syntax corrected to:
=SumByColor(B1,B2:B100)/CO
Dave
Surely you need to count by colour too?
@rorya - I had thought about that, and was unclear to the Asker's intent.
To clarify:
If you want to SUM the Color-coded cells, dividing by the count of all cells in the range, then use this formula:
=SumByColor(B1,B2:B100)/CO UNT(B2:B10 0)
If you want to SUM the Color-coded cells, dividing by the count of the color-coded cells, then use this formula:
=SumByColor(B1,B2:B100)/Co untByColor (B1,B2:B10 0)
Again, where B1 (in this example) is color coded the same way you're screening your data in range B2:B100
Dave
To clarify:
If you want to SUM the Color-coded cells, dividing by the count of all cells in the range, then use this formula:
=SumByColor(B1,B2:B100)/CO
If you want to SUM the Color-coded cells, dividing by the count of the color-coded cells, then use this formula:
=SumByColor(B1,B2:B100)/Co
Again, where B1 (in this example) is color coded the same way you're screening your data in range B2:B100
Dave
As the asker wants an average of yellow cells, it presumably has to be the latter option.
Either way, it would be a lot simpler and more reliable to use an additional column and enter say an 'x' value and then average based on that to avoid the recalc problems with using coloured cells as data.
Either way, it would be a lot simpler and more reliable to use an additional column and enter say an 'x' value and then average based on that to avoid the recalc problems with using coloured cells as data.
ASKER
I think I will only need to "count" the cells if there is a value in them, correct? So far, I have come up with the following, but it seems to hang-up when the range in which it is looking for the color yellow, does not have any yellow cells:
Public Function AverageYellow(ByVal Source As Range)
Dim Cell As Range
Dim Total As Double
Dim Count As Long
Application.Volatile
For Each Cell In Source
If Cell.Interior.ColorIndex = 6 And Cell.Value <> 0 Then
Total = Total + Cell.Value
Count = Count + 1
End If
Next Cell
AverageYellow = Total / Count
End Function
AND:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Worksheets("A1").Range("AV 3").Value = AverageYellow(ActiveSheet. Range("I3: I500"))
Worksheets("A1").Range("AW 3").Value = AverageYellow(ActiveSheet. Range("S3: S500"))
Worksheets("A1").Range("AX 3").Value = AverageYellow(ActiveSheet. Range("X3: X500"))
Worksheets("A1").Range("AY 3").Value = AverageYellow(ActiveSheet. Range("AD3 :AD500"))
Worksheets("A1").Range("AZ 3").Value = AverageYellow(ActiveSheet. Range("AI3 :AI500"))
Worksheets("A1").Range("BA 3").Value = AverageYellow(ActiveSheet. Range("AQ3 :AQ500"))
Worksheets("A1").Range("BB 3").Value = AverageYellow(ActiveSheet. Range("AS3 :AS500"))
Worksheets("A1").Range("AV 4").Value = AverageRed(ActiveSheet.Ran ge("C3:H50 0"))
Worksheets("A1").Range("AW 4").Value = AverageRed(ActiveSheet.Ran ge("K3:R50 0"))
Worksheets("A1").Range("AX 4").Value = AverageRed(ActiveSheet.Ran ge("U3:W50 0"))
Worksheets("A1").Range("AY 4").Value = AverageRed(ActiveSheet.Ran ge("Z3:AC5 00"))
Worksheets("A1").Range("AZ 4").Value = AverageRed(ActiveSheet.Ran ge("AF3:AH 500"))
Worksheets("A1").Range("BA 4").Value = AverageRed(ActiveSheet.Ran ge("AK3:AP 500"))
Worksheets("A1").Range("BB 4").Value = AverageRed(ActiveSheet.Ran ge("AS3:AS 500"))
END SUB
Public Function AverageYellow(ByVal Source As Range)
Dim Cell As Range
Dim Total As Double
Dim Count As Long
Application.Volatile
For Each Cell In Source
If Cell.Interior.ColorIndex = 6 And Cell.Value <> 0 Then
Total = Total + Cell.Value
Count = Count + 1
End If
Next Cell
AverageYellow = Total / Count
End Function
AND:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Worksheets("A1").Range("AV
Worksheets("A1").Range("AW
Worksheets("A1").Range("AX
Worksheets("A1").Range("AY
Worksheets("A1").Range("AZ
Worksheets("A1").Range("BA
Worksheets("A1").Range("BB
Worksheets("A1").Range("AV
Worksheets("A1").Range("AW
Worksheets("A1").Range("AX
Worksheets("A1").Range("AY
Worksheets("A1").Range("AZ
Worksheets("A1").Range("BA
Worksheets("A1").Range("BB
END SUB
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you both. As there were many sheets, with different variables, I used a comblination of both solutions to come up with the following (I'm only including two snippets of the code as it is overall quite a comblination of code and variables):
Public Function AverageYellow(ByVal Source As Range)
Dim Cell As Range
Dim Total As Double
Dim Count As Long
Application.Volatile
For Each Cell In Source
If Cell.Interior.ColorIndex = 6 And Cell.Value > 0 Then
Total = Total + Cell.Value
Count = Count + 1
End If
Next Cell
If Count > 0 Then
AverageYellow = Total / Count
Else
AverageYellow = 0
End If
End Function
Public Function AverageRed(ByVal Source As Range)
Dim Cell As Range
Dim Total As Double
Dim Count As Long
Application.Volatile
For Each Cell In Source
If Cell.Interior.ColorIndex = 3 And Cell.Value > 0 Then
Total = Total + Cell.Value
Count = Count + 1
Else
Total = Total + 0
Count = Count + 0
End If
Next Cell
If Count > 0 Then
AverageRed = Total / Count
Else
AverageRed = 0
End If
End Function
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Worksheets("BH").Range("AV 3").Value = AverageYellow(Sheets("BH") .Range("I3 :I500"))
Worksheets("BH").Range("AW 3").Value = AverageYellow(Sheets("BH") .Range("S3 :S500"))
Worksheets("BH").Range("AX 3").Value = AverageYellow(Sheets("BH") .Range("X3 :X500"))
Worksheets("BH").Range("AY 3").Value = AverageYellow(Sheets("BH") .Range("AD 3:AD500"))
Worksheets("BH").Range("AZ 3").Value = AverageYellow(Sheets("BH") .Range("AI 3:AI500"))
Worksheets("BH").Range("BA 3").Value = AverageYellow(Sheets("BH") .Range("AQ 3:AQ500"))
Worksheets("BH").Range("BB 3").Value = AverageYellow(Sheets("BH") .Range("AS 3:AS500"))
Worksheets("BH").Range("AV 4").Value = AverageRed(Sheets("BH").Ra nge("C3:H5 00"))
Worksheets("BH").Range("AW 4").Value = AverageRed(Sheets("BH").Ra nge("K3:R5 00"))
Worksheets("BH").Range("AX 4").Value = AverageRed(Sheets("BH").Ra nge("U3:W5 00"))
Worksheets("BH").Range("AY 4").Value = AverageRed(Sheets("BH").Ra nge("Z3:AC 500"))
Worksheets("BH").Range("AZ 4").Value = AverageRed(Sheets("BH").Ra nge("AF3:A H500"))
Worksheets("BH").Range("BA 4").Value = AverageRed(Sheets("BH").Ra nge("AK3:A P500"))
Worksheets("BH").Range("BB 4").Value = AverageRed(Sheets("BH").Ra nge("AS3:A S500"))
Worksheets("A1").Range("AV 3").Value = AverageYellow(Sheets("A1") .Range("I3 :I500"))
Worksheets("A1").Range("AW 3").Value = AverageYellow(Sheets("A1") .Range("S3 :S500"))
Worksheets("A1").Range("AX 3").Value = AverageYellow(Sheets("A1") .Range("X3 :X500"))
Worksheets("A1").Range("AY 3").Value = AverageYellow(Sheets("A1") .Range("AD 3:AD500"))
Worksheets("A1").Range("AZ 3").Value = AverageYellow(Sheets("A1") .Range("AI 3:AI500"))
Worksheets("A1").Range("BA 3").Value = AverageYellow(Sheets("A1") .Range("AQ 3:AQ500"))
Worksheets("A1").Range("BB 3").Value = AverageYellow(Sheets("A1") .Range("AS 3:AS500"))
Worksheets("A1").Range("AV 4").Value = AverageRed(Sheets("A1").Ra nge("C3:H5 00"))
Worksheets("A1").Range("AW 4").Value = AverageRed(Sheets("A1").Ra nge("K3:R5 00"))
Worksheets("A1").Range("AX 4").Value = AverageRed(Sheets("A1").Ra nge("U3:W5 00"))
Worksheets("A1").Range("AY 4").Value = AverageRed(Sheets("A1").Ra nge("Z3:AC 500"))
Worksheets("A1").Range("AZ 4").Value = AverageRed(Sheets("A1").Ra nge("AF3:A H500"))
Worksheets("A1").Range("BA 4").Value = AverageRed(Sheets("A1").Ra nge("AK3:A P500"))
Worksheets("A1").Range("BB 4").Value = AverageRed(Sheets("A1").Ra nge("AS3:A S500"))
END SUB
Public Function AverageYellow(ByVal Source As Range)
Dim Cell As Range
Dim Total As Double
Dim Count As Long
Application.Volatile
For Each Cell In Source
If Cell.Interior.ColorIndex = 6 And Cell.Value > 0 Then
Total = Total + Cell.Value
Count = Count + 1
End If
Next Cell
If Count > 0 Then
AverageYellow = Total / Count
Else
AverageYellow = 0
End If
End Function
Public Function AverageRed(ByVal Source As Range)
Dim Cell As Range
Dim Total As Double
Dim Count As Long
Application.Volatile
For Each Cell In Source
If Cell.Interior.ColorIndex = 3 And Cell.Value > 0 Then
Total = Total + Cell.Value
Count = Count + 1
Else
Total = Total + 0
Count = Count + 0
End If
Next Cell
If Count > 0 Then
AverageRed = Total / Count
Else
AverageRed = 0
End If
End Function
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Worksheets("BH").Range("AV
Worksheets("BH").Range("AW
Worksheets("BH").Range("AX
Worksheets("BH").Range("AY
Worksheets("BH").Range("AZ
Worksheets("BH").Range("BA
Worksheets("BH").Range("BB
Worksheets("BH").Range("AV
Worksheets("BH").Range("AW
Worksheets("BH").Range("AX
Worksheets("BH").Range("AY
Worksheets("BH").Range("AZ
Worksheets("BH").Range("BA
Worksheets("BH").Range("BB
Worksheets("A1").Range("AV
Worksheets("A1").Range("AW
Worksheets("A1").Range("AX
Worksheets("A1").Range("AY
Worksheets("A1").Range("AZ
Worksheets("A1").Range("BA
Worksheets("A1").Range("BB
Worksheets("A1").Range("AV
Worksheets("A1").Range("AW
Worksheets("A1").Range("AX
Worksheets("A1").Range("AY
Worksheets("A1").Range("AZ
Worksheets("A1").Range("BA
Worksheets("A1").Range("BB
END SUB