Can't get my UDF to return a decimal - Sum Value by color index

MasterK

New Member
Joined
Jan 15, 2015
Messages
15
I need to sum the values of a range by the background color of the cell. So I created 2 UDFs. The first one that creates the color index works fine:

VBA Code:
Function ColorIndex(CellColor As Range)
ColorIndex = CellColor.Interior.ColorIndex
End Function

The Second UDF works fine as long as we're summing whole numbers, but will not sum decimals. My suspicion is partly due to line 3 "As Integer", but I changed that to
Single, Double, Long. No change. A cell entry of 0.5 returns a "0" and 1.5 returns "2". I only need to be as precise as 1 decimal (tenths) place.
VBA Code:
Function SumByColor(CellColor As Range, rRange As Range)
Dim cSum As Long
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
  If cl.Interior.ColorIndex = ColIndex Then
    cSum = WorksheetFunction.Sum(cl, cSum)
  End If
Next cl
SumByColor = cSum
End Function

I appreciate any assistance.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try this slight variation:
VBA Code:
Function SumByColor(CellColor As Range, rRange As Range)
Dim cSum As Long
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
cSum=0
For Each cl In rRange
  If cl.Interior.ColorIndex = ColIndex Then
    cSum = cSum+cl.Value
  End If
Next cl
SumByColor = cSum
End Function
 
Upvote 0
Try declaring both your function and your sum variables as Single or Double, i.e.
VBA Code:
Function SumByColor(CellColor As Range, rRange As Range) as Double
Dim cSum As Double
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
  If cl.Interior.ColorIndex = ColIndex Then
    cSum = WorksheetFunction.Sum(cl, cSum)
  End If
Next cl
SumByColor = cSum
End Function
 
Upvote 0
Some recommendations:

Do not name functions with the same name of an attribute or method, it may conflict.
Function ColorIndex(CellColor As Range)
ColorIndex = CellColor.Interior.ColorIndex
End Function

In the second function, it is not necessary to declare the cSum and ColIndex variables, you can perform the sum directly, in fact the variable cl is the only one that you had to declare, it can be as follows:

VBA Code:
Function SumByColor(CellColor As Range, rRange As Range) As Double
  Dim cl As Range
  For Each cl In rRange
    If cl.Interior.ColorIndex = CellColor.Interior.ColorIndex Then
      SumByColor = SumByColor + cl.Value
    End If
  Next cl
End Function
 
Upvote 0
Try declaring both your function and your sum variables as Single or Double, i.e.
VBA Code:
Function SumByColor(CellColor As Range, rRange As Range) as Double
Dim cSum As Double
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
  If cl.Interior.ColorIndex = ColIndex Then
    cSum = WorksheetFunction.Sum(cl, cSum)
  End If
Next cl
SumByColor = cSum
End Function

This did it. I was only changing one of them.
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,392
Members
449,081
Latest member
JAMES KECULAH

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