Performing calculations with cells with same fill color and containing letters to which have been assigned numerical values. I need a super MS Excel 2

dellemara

New Member
Joined
Oct 31, 2012
Messages
12
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.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Actually, I think you will find that your formula doesn't work because the asterisk in A* is treated as a wildcard. So A will be counted twice.

Maybe you can use Chip Pearson's ColorIndexOfRange function somehow:

Color Functions In Excel
 
Upvote 0
Dear Mr Poulsom, you were absolutely right as the A* (which represents a mark and has been assigned a numerical value of 140) is not treated as a string as I thought it would, because of the speech marks. I could still use this formula even if it doesn't achieve entirely my object as described in my original message. How would I get around the *, wild card problem in this case?

Also thank your for your link which will be extremely useful for few different applications I have in mind. The problem I am left with, at the moment, is that I can calculate the sum of the content of a range of cells with same fill color, only if all the cells in that range contain numbers but not if they contain, each, a letter to which have been assigned a numerical value.

Sorry about the convoluted explanation, the problem is that I am Italian and English is therefore not my first language. I would really appreciate some further help on this matter. Many thanks.
 
Upvote 0
You can overcoming the double counting by preceding the asterisk with a tilde (the escape character):

=SUMPRODUCT(COUNTIF(F2:AI2,{"A~*","A","B","C","D","E"}),{140,120,100,80,60,40})
 
Upvote 0
Here's a custom worksheet function that you could try.

Code:
Function SumColorLookups(rMatchColor As Range, rSumRange As Range, _
    rLookup As Range) As Variant

    
    Dim c As Range
    Dim lColor As Long
    Dim dSumTotal As Double, dVal As Double

    
    Application.Volatile True

    
    lColor = rMatchColor.Interior.Color

    
    On Error Resume Next
    For Each c In rSumRange
        If c.Text <> "" Then
            If c.Interior.Color = lColor Then
                dVal = Application.VLookup(Replace(c.Text, "*", "~*"), rLookup, 2, 0)
                If Err.Number = 0 Then
                    dSumTotal = dSumTotal + dVal
                Else
                    Err.Clear
                End If
            End If
        End If
    Next c
    On Error GoTo 0
    SumColorLookups = dSumTotal

End Function

It assumes that you set up a lookup table (you could do this in place of your existing legend or in a hidden range).
Excel Workbook
BC
20Points Awarded
21A*140
22A120
23B100
24C80
25D60
26E40
Sheet


It also assumes you reference a cells that has the matching color. In this example, it shows those above your totals headers.
Excel Workbook
AIAJ
3
4Total (only values in cells with white fill color)Total (only values in cells with red fill color)
5480120
626080
716060
83800
932060
Sheet
 
Upvote 0
Many, many, many thanks, the macro and formulas you provided in your messages work perfectly well. I am so grateful for your time and efforts.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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