My function works but returns #VALUE [Excel 2003]

Dynamo Nath

Board Regular
Joined
Aug 5, 2009
Messages
142
I've been trying to write a function to simplify finding the number of unique items in a list. And it works too except for the fact that it won't put the answer in the cell! Clicking the fx button to access the function screen and entering the range to check shows the anticipated result at the bottom but pressing ok doesn't transfer the result to the cell.

Code:
Public Function myUniqueDataCount(data_range)

    Dim wibble As Range
    Dim myrange As String
    Dim myblanks As Integer
    Dim myanswer As Integer
    
    myblanks = Application.WorksheetFunction.CountBlank(data_range)
    
    data_range.Name = "data_range"
    
    If myblanks > 0 Then
        myUniqueDataCount = "Data contains empty fields"
        Exit Function
        Else:
        myanswer = Application.Evaluate("=SUM(IF(FREQUENCY((MATCH(data_range,data_range,0)), MATCH(data_range,data_range,0))>0,1))")
    End If
    
    myUniqueDataCount = myanswer 'this line might not be required but it was an attempt to resolve the problem.
    
End Function

I'm sure it's fairly straight forwards but I'm fresh out of ideas. Thanks in advance, Nathan.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You say the function "works but returns #VALUE". That suggests it's not working.

If you set a breakpoint at the top of the function code and step through it you'll see it fails at this line:-
Code:
data_range.Name = "data_range"

I don't think you can name a range like that. Don't you have to use Names.Add?
 
Upvote 0
You can name a range that way, but you can't name a range from a UDF. You could use:
Code:
Public Function myUniqueDataCount(data_range As Range)

    Dim wibble As Range
    Dim myrange As String
    Dim myblanks As Long
    Dim myanswer As Long
    Dim strAddy As String
    strAddy = data_range.Address
    
    myblanks = Application.WorksheetFunction.CountBlank(data_range)
    
    If myblanks > 0 Then
        myUniqueDataCount = "Data contains empty fields"
        Exit Function
        Else:
        myanswer = Application.Evaluate("=SUM(IF(FREQUENCY((MATCH(" & strAddy & "," & strAddy & ",0)), MATCH(" & strAddy & "," & strAddy & ",0))>0,1))")
    End If
    
    myUniqueDataCount = myanswer 'this line might not be required but it was an attempt to resolve the problem.
    
End Function
 
Upvote 0
Hi, thanks for the reply.

By stepping through the function wouldn't you be missing out the step where the argument is collected? Stepping through will mean that the data_range in brackets next to the function name will be empty.

Deleting the named range and by calling the function from a worksheet cell and stepping through I can't replicate your error. The answer appears in the function arguments dialogue box next to formula result but clicking ok doesn't return it :(

Naming the range in that fashion seems to work for me...
 
Upvote 0
Rory,

thanks for that I shall go try it and let you know. I'd come to the conclusion about the UDF and renaming it/storing it as a worksheet named range that the function would reference was my attempt at a solution. Maybe I should have called them different names to avoid confusion in my code; kinda makes sense to me.
 
Upvote 0
Rory,

Thanks very much that worked a treat. Reading the help file on "address" now sheds light on the phrase "..in the language of the macro" which I was struggling with before as was not sure what it meant but came up in the definition of something else when searching the help for clues.
 
Upvote 0
Because you need to concatenate the value of strAddy into the formula string that is being evaluated.
 
Upvote 0
Doh! Of course! I mis-read why you put the " in to it. I'm so glad it's Friday!

Out of curiosity, do you know why in my original function, clicking in the formula bar on the cell with the function in and pressing F9 appears to return the result but takes out the function, ie just leaving the result.

many thanks for all your help.
 
Upvote 0
f9 evaluates the formula. It works that way with any function.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,813
Members
452,945
Latest member
Bib195

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