HELP PLEASE !!!!!!
I need to know if there is a way to sum all the values WHERE the font is red?
THANKS
![]() |
Patch reliability is unclear, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » CONDITIONAL
A function to count based on font color was posted recently by Hans Pottel, and I am taking the liberty of posting an amended version to sum.
Function SumColors(R As Range, Col As Integer) As Integer Application.Volatile True Dim cell As Range SumColors = 0 For Each cell In R If cell.Font.ColorIndex = Col Then SumColors = SumColors + cell.Value End If Next End Function
Place that function in a workbook module and use the following syntax =SumColors(Range,Color number) , e.g. =SumColors(A1:A9,3) will sum all the cells in A1 to A9 if they are colored red.
Some caveats :
1. Conditional formatting is ignored, the underlying color of the cell is all that matters.
2. If you change the font colors of one of the numbers you must recalculate manually (F9) as Excel does not do recalculations automatically for changes in formatting.
If you want to sum cells based on a color applied by conditioanl formatting, then try to use a SUMIF with the same criteria.
Andrew
As I mentioned before, You can change my code as you wish, but don’t blame me for it … (just joking)
I was just wondering why you were changing the return of the function to a ‘double’ type in case of the CountColors function: if you count the cells with a specific interior color then the result is an integer or if you have plenty to count you can change it to ‘long’.
In case you want to sum the contents of the cells with a specific interior color, then it is quite obvious that you change the return value to ‘double’ as Excel cells are all by default of the type ‘double’. You can easily check that with the Vartype command from VBA.
So, Rory, use my code, but when someone changes it, blame the one who made the changes… Sorry Andrew..
Hans,
I didn’d change the return of CountColors, but unfortunately omitted to change it when the function changed from counting to summing. I just copied and pasted, I intended removing AS Integer, as clearly it is not suitable for a general purpose SUM function, but after making the other changes I just forgot. I should have composed the thing from scratch I suppose.
Andrew
Andrew,
I’ve just read through this CONDITIONAL thread, by accident, and saw my name (very nice of you to mention the original author), that’s why I couldn’t stand to post a reply, or, is it because it’s a quiet Saterday afternoon and I was tired of working in the garden, now that it stopped raining?
Try putting in a general module, the place where recorded macros are located. In the VB editor, select the workbook you want the code in, go to Insert, Module, and place it there. Change the first line to (See Rory’s post above) :-
Function SumColors(R As Range, Col As Integer) As Double
Hope that works for you. ( and remember always that it will not automatically update)
Andrew
Jim, sorry but your request for the count routine got overlooked. The equivalent code is at Countif.
However I am including a matching set that sum and count based on either font color or cell color.
Function CellColorCount(fRange As Range, fCol) As Long Application.Volatile True Dim Rng As Range For Each Rng In fRange If Rng.Interior.ColorIndex = fCol And Rng.Value "" Then CellColorCount = CellColorCount + 1 End If Next End Function Function CellColorSum(fRange As Range, fCol) As Double Application.Volatile True Dim Rng As Range For Each Rng In fRange If Rng.Interior.ColorIndex = fCol Then CellColorSum = CellColorSum + Rng.Value End If Next End Function Function FontColorCount(fRange As Range, fCol) As Long Application.Volatile True Dim Rng As Range For Each Rng In fRange If Rng.Font.ColorIndex = fCol And Rng.Value "" Then FontColorCount = FontColorCount + 1 End If Next End Function Function FontColorSum(fRange As Range, fCol) As Double Application.Volatile True Dim Rng As Range For Each Rng In fRange If Rng.Font.ColorIndex = fCol Then FontColorSum = FontColorSum + Rng.Value End If Next End Function
These functions only update when the worksheet is recalculated (F9). As a change in formatting does not trigger excel’s recalc, they will not be updated purely on a color change. The following code may help get round that problem, if it is suitable for your circumstances :-
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) Calculate End Sub
That code should be placed in the Workbook module of the project involved, and not in a general module.
Also note that the Count functions only count non-blank cells.
All are called by FunctionName(Range,ColorIndex code)
Hope they are of use.
Hi Andrew,
I was copying these *very useful* functions into my personal library when I noticed two typos. So in case anyone else wants to do the same I thought I’d let you know.
Function CellColorCount(fRange As Range, fCol) As Long Application.Volatile True Dim Rng As Range For Each Rng In fRange If Rng.Interior.ColorIndex = fCol And Rng.Value "" Then ColorCount = ColorCount + 1 End If Next End Function
Function CellColorSum(fRange As Range, fCol) As Double Application.Volatile True Dim Rng As Range For Each Rng In fRange If Rng.Interior.ColorIndex = fCol Then ColorSum = ColorSum + Rng.Value End If Next End Function
The lines reading
ColorSum = ColorSum + Rng.Value
ColorCount = ColorCount + 1
should read
CellColorSum = CellColorSum + Rng.Value
CellColorCount = CellColorCount + 1
I appreciate all the effort you put in here at the Lounge to help out those less knowledgeable like myself.
Ken
P.S. What does Application.Volatile True do?
Thanks Ken, thats what I get for not being attentive – I changed the names of the functions and forgot the obvious changes that should have been made in the code.
The Application.Volatile statement is to ensure that Excel recalculates the function any time a change is made to the sheet that involves recalculation. Strictly speaking it is not necessary as a function is recalculated any time any of it’s arguments change. However in the case of these functions no calculation will take place if the color of a cell or font changes, as Excel does not do a recalculation based on formatting events. So if the volatile method is invoked it helps to keep the result more up to date if some other unconnected calculation takes place.
Sorry for any trouble caused,
Andrew
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications