Summing XColors Using UDF Function

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,601
Office Version
  1. 2021
Platform
  1. Windows
I have used the following code to sum color using UDF in Excel, but the result I get is zero.

See my code below as well as sample data. It would be appreciated if someone could assist me

Function SumColor(rColor As Range, rSumRange As Range)

Dim rCell As Range

Dim iCol As Integer

Dim vResult



iCol = rColor.Interior.ColorIndex



For Each rCell In rSumRange

If rCell.Interior.ColorIndex = iCol Then

vResult = vResult + 1

End If

Next rCell



CountColor = vResult

End Function

Summing Colors.xltm
ABCD
1125.25
2185.95
3106.75
4103.75
599.85
6
70
8
Sheet1
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Your code is fine...but you've got some naming problems..

Rich (BB code):
Function SumColor(rColor As Range, rSumRange As Range)

CountColor = vResult

End Function

Those both need to be the same...
 
Upvote 0
Hi Jonmo1

Thanks for pointing out the error in my code. I have corrected this and attached sample data, but when summing the colored items, the result is zero. Please check & advise

Function SumColor(rColor As Range, rSumRange As Range)

Dim rCell As Range

Dim iCol As Integer

Dim vResult



iCol = rColor.Interior.ColorIndex



For Each rCell In rSumRange

If rCell.Interior.ColorIndex = iCol Then

vResult = vResult + 1

End If

Next rCell



SumColor = vResult

End Function




<b>Sheet1</b><br /><br /><table border="1" cellspacing="0" cellpadding="0" style="font-family:Calibri,Arial; font-size:11pt; background-color:#ffffff; padding-left:2pt; padding-right:2pt; "> <colgroup><col style="font-weight:bold; width:30px; " /><col style="width:64px;" /></colgroup><tr style="background-color:#cacaca; text-align:center; font-weight:bold; font-size:8pt; "><td >*</td><td >A</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >1</td><td style="text-align:right; ">125.25</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >2</td><td style="background-color:#0066cc; text-align:right; ">185.95</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >3</td><td style="text-align:right; ">106.75</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >4</td><td style="background-color:#0066cc; text-align:right; ">103.75</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >5</td><td style="text-align:right; ">99.85</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >6</td><td >*</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >7</td><td style="text-align:right; ">0</td></tr><tr style="height:18px ;" ><td style="font-size:8pt; background-color:#cacaca; text-align:center; " >8</td><td >*</td></tr></table><br /><table style="font-family:Arial; font-size:10pt; border-style: groove ;border-color:#00ff00;background-color:#fffcf9; color:#000000; "><tr><td ><b>Spreadsheet Formulas</b></td></tr><tr><td ><table border = "1" cellspacing="0" cellpadding="2" style="font-family:Arial; font-size:9pt;"><tr style="background-color:#cacaca; font-size:10pt;"><td >Cell</td><td >Formula</td></tr><tr><td >A7</td><td >=sumcolor(A2,A1:A6)</td></tr></table></td></tr></table> <br />Excel tables to the web - Excel Jeanie Html 4
 
Upvote 0
Hello

That works for me, but it only counts the coloured cells, it returns 2.

To sum the range change this line of code, as below.

Code:
vResult = vResult + rCell.Value
 
Upvote 0
Are your cells colored via conditional formatting?
If so, this function will not work.

If this is the case, you'd be better off designing your UDF to count the condition that causes the color, not the color itself.
 
Upvote 0
Hi Brian,
Your function would be:
Rich (BB code):

Function SumColor(rColor As Range, rSumRange As Range) As Double
  
  ' This calls the function each time the cells change or sheet calculates
  Application.Volatile
  
  ' Main
  Dim rCell As Range, iCol As Integer
  iCol = rColor.Interior.ColorIndex
  For Each rCell In rSumRange
    If rCell.Interior.ColorIndex = iCol Then
      SumColor = SumColor + rCell.Value
    End If
  Next
  
End Function

Please take into account that changing of the cells colors doesn't raise recalculation of this UDF.
Changing of any cells values or F9 pressing is required for updating of UDF result.
 
Last edited:
Upvote 0
Hi Guys

Thanks for the help. The UDF function now works perfectly
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,792
Members
452,942
Latest member
VijayNewtoExcel

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