UDF not working

NMitch

New Member
Joined
Feb 19, 2014
Messages
19
Hi all,

This is for Excel 2013, and here is my code, which has worked before:

Code:
Public Function SumByColor(CellColor As Range, rRange As Range)    
    '   CellColor is the cell containing they colour type you are searching for
    '   rRange is the range of cells to add, if the colour matches
    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(c1, cSum)
        End If
     Next cl
     SumByColor = cSum
End Function

When using this in a macro-enabled workbook, the #NAME ? result appears.

The function used to be just a normal function, which resides in my PERSONAL.XLSB, but changed to PUBLIC to see if that made a difference.

I have a habit of doing things, like formulae, in lower case, then seeing if it changes to the proper case after pressing Enter. The UDF is SumByColor(), but the formula shows sumbycolor().

Can someone let me know what I'm doing wrong?

Thank you,
 
Last edited:
Another possible problem:
Are the cells color manually or by conditional formatting?
If the latter, the UDF will not work, because a UDF does not "see" colored cells by CF.
However a Sub can "see" them using the DisplayFormat property.

M.
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Well spotted, Marcelo! Thank you! ;)

Now I have numbers, which seem to add up precisely!
 
Last edited:
Upvote 0
Thanks, Marcelo, but no, they are not coloured by conditional formatting.

What I do now have as a problem is the SUMBYCOLOR() is rounding to the nearest $.

Should cSum be declared differently?

Thank you
 
Upvote 0
Ok, I think I've found the answer. Instead of declaring cSum as Integer, I've used Double, and it seems to be giving me the right answer.

I will need this variable to hold numbers to two decimal points, up to around 300m.
 
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