how to count and sum cells by color in excel

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,259
Office Version
  1. 2010
Platform
  1. Windows
Hi I have the formula below where I am trying to count how many cells have a colour and counting, where my range is to a and the cell colour is yellow which is p1, but the code doesn't work I get a #NAME ? please can u help?

=ASAPCOUNTBYCELLCOLOR(A:A,P1)
 
If its for summing up,

Code:
Public Function ASAPSumByCellColor(rngSource As Range, sColorIndex As Single) As Single                                   
                                   
    Dim rngCel As Range
    
  Application.Volatile
  N = 0
  For Each rngCel In rngSource
    If rngCel.Interior.colorindex = sColorIndex Then
      If IsNumeric(rngCel.Value) Then N = N + rngCel.Value
    End If
  Next
  ASAPSumByCellColor = N
End Function
[TABLE="width: 85%"]
<tbody></tbody>[/TABLE]


Excel 2013/2016
ABCDE
1
235473168
36577
44355
513
614
729
Sheet4
Cell Formulas
RangeFormula
E2=ASAPSumByCellColor($A$2:$B$7,D2)
 
Last edited:
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi thanks for your help. But unfortunately it was to count colours on cells. I managed to get the code for the asap formula but it slowed the spreadsheet down alot so I deleted that formula/code now as no good for me.
 
Upvote 0
How are the cells getting coloured?
 
Upvote 0
Hi I just click on the cell then fill cell with a colour. I haven't used conditional formatting.
 
Upvote 0
Is there anything in those cells to indicate what colour it should be?
 
Upvote 0
Hi. No nothing. For example all it is I run a report and highlight the line if important yo make it stand out
 
Upvote 0
In that case you will need a macro or the UDF to do what you want, but it won't update automatically because Xl does not recognise the colour of a cell being changed.
You would be better of putting something like an "X" in cell as well as changing the colour. That way you could use a normal countif formula.
 
Upvote 0
Hi thanks for the advice. I think I will go with your idea much easier. Thank you.
 
Upvote 0
You're welcome & thanks for the feedback.
Using colour as data is something that's best avoided as it can cause all sorts of problems.
 
Upvote 0
Thank you. Your feedback and help is always fantastic :) I shall definitely do what you recommend.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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