Serving the Quantitative Finance Community

 
User avatar
Beachcomber
Topic Author
Posts: 2
Joined: May 25th, 2004, 5:56 pm

can't convert a formula in excel to vba code

June 13th, 2015, 4:37 pm

Hi,I am attempting to count the number of unique string variables in a list (for example, let's say NFL team names). I found this formula that works just fine in excel...=SUMPRODUCT((A2:A100 <> "")/COUNTIF(A2:A100,A2:A100 & ""))I am now attempting to put this logic into vba via...=APPLICATION.WORKSHEETFUNCTION.SUMPRODUCT((A2:A100 <> "")/APPLICATION.WORKSHEETFUNCTION.COUNTIF(A2:A100,A2:A100 & "")). I get the following error message...Compile error: Type Mismatch and the <> is highlighted.I think I must be dimensioning the variables (the list of team names is the most likely candidate) incorrectly, but I have tried string, variant, and several others and I can't get rid of the error message.I'm sure I'm doing something stupid. Can someone help me out?Thanks,Beachcomber
 
User avatar
tags
Posts: 3603
Joined: February 21st, 2010, 12:58 pm

can't convert a formula in excel to vba code

June 13th, 2015, 5:44 pm

[SUMPRODUCT(($A$2:$A$100<>"")/ COUNTIF($A$2:$A$100, $A$2:$A$100 & ""))]
 
User avatar
Beachcomber
Topic Author
Posts: 2
Joined: May 25th, 2004, 5:56 pm

can't convert a formula in excel to vba code

June 14th, 2015, 6:33 pm

Thank you for the help, but I cannot get the formula to work even with your advice.I may have to try something different.Beachcomber
 
User avatar
tags
Posts: 3603
Joined: February 21st, 2010, 12:58 pm

can't convert a formula in excel to vba code

June 14th, 2015, 6:46 pm

It works here. See the attached file.But maybe I don't get what you want to do.
Attachments
NFL.zip
(11.71 KiB) Downloaded 94 times
 
User avatar
bearish
Posts: 5906
Joined: February 3rd, 2011, 2:19 pm

can't convert a formula in excel to vba code

June 14th, 2015, 7:32 pm

QuoteOriginally posted by: tagomaIt works here. See the attached file.But maybe I don't get what you want to do.That works, and is probably more or less what OP called for, but it is doubly ugly. Not only does it call specific cell addresses from VBA (which breaks the Excel recalculation logic and is not robust to changes in the sheet layout), but you do it inside a function, thus breaking somebody's rule that functions should only take arguments that are explicitly passed to them. I am interested in understanding why it works, since there is some feature of VBA at work here that I was not aware of.
 
User avatar
Beachcomber
Topic Author
Posts: 2
Joined: May 25th, 2004, 5:56 pm

can't convert a formula in excel to vba code

June 14th, 2015, 9:23 pm

Thanks. That works. The difference is that I used APPLICATION.WORKSHEETFUNCTION.SUMPRODUCT instead of SUMPRODUCT.The fixed cell range was only for demonstration purposes. I am defining and populating a variable to work with inside VBA.I appreciate the help.Beachcomber