Using EVALUATE in UDF on an array formula giving error

  • Hi everyone,


    I am working on a UDF that will basically function as a "countifs" but only counting unique values. I think I am on the right track, however I can't get the "EVALUATE" function to operate properly on:


    Code
    Msgbox Evaluate("=SUM(IF(FREQUENCY(IF(" & OptArg1 & ",MATCH(" & RtrRng & "," & RtrRng & ",0)),ROW(INDIRECT(""1:""&ROWS(" & RtrRng & "))))>0,1))")


    This is the full code:



    Any thoughts???


    Thanks :)

  • Re: Using EVALUATE in UDF on an array formula giving error


    I should mention that I am in now way set on using the above method. All I need is a UDF to return the count of unique values in "ReturnRng" based on multiple (also optional) criteria and criteria ranges to a limit of 10.


    I just haven't been able to find anything so far that can do what I need, which is what started this whole problem in the first place.

  • Re: Using EVALUATE in UDF on an array formula giving error


    Hi Norie,


    ...I don't need the message box, it is simply there for now while I try and figure out why the evalute portion isn't returning a valid result.


    I need the evaluate portion because based on the criteria, search ranges, and result range that is passed in from the udf by the user, the result of my UDF will be a count of unique values in the return range based on the criteria that the user chooses...and to return the count of unique values based on multiple criteria, I currently only know how to do with an array formula which is why I am trying to use "evaluate" to capture that number.

  • Re: Using EVALUATE in UDF on an array formula giving error


    Sorry you've lost me.


    If you are going to return the result from the Evaluate why are you showing, or at least trying to, it in a message box and not assigning it to CountUniqueMultiple?

    Boo!:yikes:

  • Re: Using EVALUATE in UDF on an array formula giving error


    Hi Norie,


    No worries, I've lost myself by now on a Friday lol.


    You're right, there is no need for the msgbox and the end result would be for me to assign the Evaluate result to CountUniqueMultiple, however this becomes sort of irrelevant because even when I assign the Evaluate result to CountUniqueMultiple I just get a #REF error returned anyways.


    Meaning...even if i do this:


    Code
    CountUniqueMultiple = Evaluate("=SUM(IF(FREQUENCY(IF(" & OptArg1 & ",MATCH(" & RtrRng & "," & RtrRng & ",0)),ROW(INDIRECT(""1:""&ROWS(" & RtrRng & "))))>0,1))")


    ...it still isn't working :(


    The bottom line is the Evaluate portion of the code isn't working, so that's what I am looking for help with :)

  • Re: Using EVALUATE in UDF on an array formula giving error


    Yes definitely checked the formula. Which is part of the reason why I was playing around with the msgbox. If I set countuniquemultiple to:


    Code
    "=SUM(IF(FREQUENCY(IF(" & OptArg1 & ",MATCH(" & RtrRng & "," & RtrRng & ",0)),ROW(INDIRECT(""1:""&ROWS(" & RtrRng & "))))>0,1))"


    So basically, the UDF spits out the string that I would be asking EVALUATE to handle.


    If I then take the string returned from the UDF and paste values, then edit the formula and confirm with CTRL+SHIFT+ENTER it works and calculates correctly.

  • Re: Using EVALUATE in UDF on an array formula giving error


    Sorry but I can't see what the problem is.


    The Evaluate is returning an Error 2015, whatever that means, but I don't know why.


    By the way, why do you have SearchRng1 repeated 10 times when setting Rng1 to Rng10?

    Boo!:yikes:

  • Re: Using EVALUATE in UDF on an array formula giving error


    Hi Norie,


    That's an error. The searchrng variable should be incrementing up with the rng variable. But because I'm only testing at 1 criteria. Right now didn't catch it. Thanks!


    Back to the issue though, thanks for trying! Hopefully someone else can shed some light if they've run in to this before. From what I can see it should be evaluating fine.

  • Re: Using EVALUATE in UDF on an array formula giving error


    I'm not too sure if putting it in a sub would change anything but will give it a shot Monday morning when I'm back on my system.


    Definitely less than ideal though. Hoping someone out there might have a work around or more elegant solution of producing what I'm looking for.

  • Re: Using EVALUATE in UDF on an array formula giving error


    Unfortunately putting this into a Sub routine had no effect, still erroring out.

  • Re: Using EVALUATE in UDF on an array formula giving error


    After trouble shooting this for a bit, it seems that this may be an issue with using INDIRECT within an EVALUATE in VBA. Still haven't resolved, but thought I'd keep the thread updated incase anyone is working on this / following along.

  • Re: Using EVALUATE in UDF on an array formula giving error


    So, to close the loop it does appear that the issue was using INDIRECT within an EVALUATE within a UDF. I cannot say why this doesn't work, but it doesn't. I have managed to work around this, and now have a functioning UDF. In case anyone is looking for something like this (I haven't seen anything else online through my searches to resolve this issue) the code is available below. This is a UDF that will return the count of unique values in a range given a variable number of criteria, and criteria ranges. There may be a more elegant way to accomplish this, but this is the best I have come up with or seen so far:


    Definitely open to suggestions and improvements :)


  • Re: Using EVALUATE in UDF on an array formula giving error


    And....first issue has come up. If you are referencing ranges outside of the workbook where the UDF is being used, the formula won't calculate unless I go to the external workbook, activate a cell (F2) and then hit enter.


    Not sure why this is coming up, but it seems to only be an issue with external workbook references.


    Edit: Further testing has revealed that this issue comes up even if you are referencing ranges that are in the same workbook as the UDF, but on different worksheets.

  • Re: Using EVALUATE in UDF on an array formula giving error


    Due to the limitations of evaluate, you might use something like this


    which allows you to pass one or more criteria pairs (value and range in that order). As written it will only work with columnar ranges but I leave that as an exercise for you.
    Haven't done a lot of testing either!

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: Using EVALUATE in UDF on an array formula giving error


    As always, you are the best!


    Seems to be working quite nicely, and early testing looks promising! I'll send you a paypal transfer later today when I get home, contribute to the coffee fund :)


    If you get a chance at some point to make it a little more robust to enable row ranges, I'll throw in a little extra :).


    Either way, thank you so much for your help!

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!