Summing based on cell color

riksutin

New Member
Joined
Mar 30, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi!

I wanted to know how I can sum up values in a cell based on it's interior color?
So I have filled in the color for these cells (range F4:F67):

1617113955827.png


And then I would like the worksheet tab to sum up the values here based on the colors, so if I change the color of one of the cells, it automatically calculates the value to the right PL (A73:A77)

1617113751195.png
 

Attachments

  • 1617113639534.png
    1617113639534.png
    8 KB · Views: 5

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
You would need to change your cell references. Can't tell exactly where your data is in the pictures. But this seems to work.

Book3
ABCD
1328
22,161PL18,313
32,342PL210,630
41,253PL314,689
52,229PL411,847
63,047
71,232
82,256
92,202
101,893
113,029
122,547
133,066
141,779
151,868
163,304
171,333
182,815
193,358
201,037
212,400
Sheet1


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim a As Range:     Set a = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)

If Not Intersect(a, Target) Is Nothing Then

    Dim r As Range:     Set r = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    Dim SD As Object:   Set SD = CreateObject("Scripting.Dictionary")
    Dim c As Range
    
    For Each c In r
        SD(c.Interior.Color) = SD(c.Interior.Color) + c.Value
    Next c
    
    Set r = Range("C2").Resize(SD.Count)
    
    r.Value = Evaluate("""PL""&" & "Row(" & r.Address & ")-1")
    Set r = r.Offset(, 1)
    r.Value = Application.Transpose(SD.items)
    
    For i = 0 To SD.Count - 1
        r.Cells(i + 1).Interior.Color = SD.keys()(i)
    Next i

End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,293
Members
449,077
Latest member
Rkmenon

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