I work in a school and need urgently a formula which would allow me to sum the content of cells with same fill colour, containing each, one letter, to which has been assigned a numerical value.
I can calculate separately:
1. The total of any range of cells containing letters with given numerical value using the SUMPRODUCT function as shown in the example below:
=SUMPRODUCT(COUNTIF(F2:AI2,{"A*","A","B","C","D","E"}),{140,120,100,80,60,40})
This formula works perfectly but does not give the chance to perform the calculation only on cells with same fill colour.
2. The total of any range of cells with same fill colour, containing numbers, thanks to a macro which enables a user defined function as shown in the example below:
Function SumByColor(InputRange As Range, ColorRange As Range) As Double
' returns the sum of each cell in the range InputRange that has the same
' background color as the cell in ColorRange
' example: =SumByColor($A$1:$A$20,B1)
' range A1:A20 is the range you want to sum
' range B1 is a cell with the background color you want to sum
Dim cl As Range, TempSum As Double, ColorIndex As Integer
' Application.Volatile ' this is optional
ColorIndex = ColorRange.Cells(1, 1).Interior.ColorIndex
TempSum = 0
On Error Resume Next ' ignore cells without values
For Each cl In InputRange.Cells
If cl.Interior.ColorIndex = ColorIndex Then
TempSum = TempSum + cl.Value
End If
Next cl
On Error GoTo 0
Set cl = Nothing
SumByColor = TempSum
End Function
The formula based on this function called SumByColor works perfectly well, see example on the use of this formula below:
=SumByColor(AC131:AC135,AC131)
The formula calculates the sum of all the cells within the range AC131:AC135 with same fill color as in the sample cell AC131.
How do I use the two functions together maybe with a nested formula, (if that is at all possible) or what formula would allow me to perform the sum as described at the beginning of this message?
I would be extremely grateful if anybody could solve this mistery for me as I do not have this level of expertise in Excel. Many thanks.
I can calculate separately:
1. The total of any range of cells containing letters with given numerical value using the SUMPRODUCT function as shown in the example below:
=SUMPRODUCT(COUNTIF(F2:AI2,{"A*","A","B","C","D","E"}),{140,120,100,80,60,40})
This formula works perfectly but does not give the chance to perform the calculation only on cells with same fill colour.
2. The total of any range of cells with same fill colour, containing numbers, thanks to a macro which enables a user defined function as shown in the example below:
Function SumByColor(InputRange As Range, ColorRange As Range) As Double
' returns the sum of each cell in the range InputRange that has the same
' background color as the cell in ColorRange
' example: =SumByColor($A$1:$A$20,B1)
' range A1:A20 is the range you want to sum
' range B1 is a cell with the background color you want to sum
Dim cl As Range, TempSum As Double, ColorIndex As Integer
' Application.Volatile ' this is optional
ColorIndex = ColorRange.Cells(1, 1).Interior.ColorIndex
TempSum = 0
On Error Resume Next ' ignore cells without values
For Each cl In InputRange.Cells
If cl.Interior.ColorIndex = ColorIndex Then
TempSum = TempSum + cl.Value
End If
Next cl
On Error GoTo 0
Set cl = Nothing
SumByColor = TempSum
End Function
The formula based on this function called SumByColor works perfectly well, see example on the use of this formula below:
=SumByColor(AC131:AC135,AC131)
The formula calculates the sum of all the cells within the range AC131:AC135 with same fill color as in the sample cell AC131.
How do I use the two functions together maybe with a nested formula, (if that is at all possible) or what formula would allow me to perform the sum as described at the beginning of this message?
I would be extremely grateful if anybody could solve this mistery for me as I do not have this level of expertise in Excel. Many thanks.