Trouble using custom function in excel

Subash.Raj

New Member
Joined
May 3, 2011
Messages
2
Hi,

I am trying to find the sum of number of cells with a particular color in a column. Decided to go for a custom function with the following code.
Function colorfunc(rColor As Range, rRange As Range, Optional SUM As Boolean)

Dim rCell As Range

Dim lCol As Long

Dim vResult

''''''''''''''''''''''''''''''''''''''
'Written by Ozgrid Business Applications
'www.ozgrid.com
'Sums or counts cells based on a specified fill color.

'''''''''''''''''''''''''''''''''''''''
lCol = rColor.Interior.ColorIndex
If SUM = True Then
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell
Else
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
End If
colorfunc = vResult

End Function

But I am facing #Name? error when trying to use the function, eventhough the function gets listed when I start typing '=' in the cell.

The function is placed in a module as shown in the screenshot below.

C:%5CUsers%5CSubashR%5CDesktop%5CColorFunc.png


I am trying to use it in the same worksheet, but not able to use it. I am able to use some simple functions but not this one.

I am getting #Ref or #Name error. I am using Excel 2010
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hi Subash,

Try with this example:
Excel Workbook
EFGH
23Sum values from cells with same fill color as E2 (Yellow)
38
41Counts cells with fill color as F5 (Red)
54
6
74
...
Excel 2010
Cell Formulas
RangeFormula
H3=colorfunc(E2,E2:F7,TRUE)
H5=colorfunc(F5,E2:F7,)


-You can sum when add the optional argument "TRUE" at the end
-You can count when you use last argument as FALSE or when is ommited

Hope this helps,

Regards
 
Upvote 0

Forum statistics

Threads
1,224,605
Messages
6,179,860
Members
452,948
Latest member
UsmanAli786

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