Best way to call UDF with ranges

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
I have written a UDF to calculate weighted ratings. It can be useful in evaluating alternative products. The user can define a collection of features that they care about, assign each one a range of values (best-worst) and a relative weight. Then for each product being evaluated, they simply fill in the value for each feature and the UDF will calculate a weighted rating.

The UDF works great. But I am having trouble figuring out the best way to call it. Before I get into that, let me demo the UDF a bit.

Here's an example of someone about to buy a car. This user has identified 5 features: (Gas Mileage, Price, Warranty, Reliability Index, and GPS). For a Gas Mileage feature, any car getting 50 mpg or more will get a top rating. Any car getting 20 mpg or less will get a bottom rating. Anything in between is prorated. The Price feature is in reverse order. Any car costing $25K or less will get a top rating. Any car costing $60K or more will get a bottom rating. The GPS feature is binary -- the car either has one or it doesn't.

Here's some sample data and the results:
C/RCDEFHIJ
3Car A
Car BCar C
4Features
BestWorstWt7.98
9.156.37
5Gas Mileage50 mpg20 mpg835 mpg43 mpg28 mpg
6Price$25K$60K7$27K$31K$46K
7Warranty5 Yrs2 Yrs43.0 Yrs5.0 Yrs3.0 Yrs
8Reliability Index02001012321
9GPS109111
10EndEndEndEndEndEndEnd

<tbody>
</tbody>
The syntax of the UDF and the actual call in H4:J4 are
Code:
=WtdRtg(Ratings, RtgsMax, RtgsMin, Weights, [ScaleMax|10], [ScaleMin|0], [OmitSw|FALSE])
=wtdrtg(H5:H9,$D$5:$D$9,$E$5:$E$9,$F$5:$F$9)
=wtdrtg(I5:I9,$D$5:$D$9,$E$5:$E$9,$F$5:$F$9)
=wtdrtg(J5:J9,$D$5:$D$9,$E$5:$E$9,$F$5:$F$9)
The first 4 parameters are ranges. The Ratings range contains the actual ratings for that product. The RtgsMax range contains the Best values. The RtgsMin range contains the Worst values. And the Weights range contains the relative weights.

The optional ScaleMax & ScaleMin parameters contain the scale that all of the ratings will be mapped onto and they default to a 0-10 scale. The OmitSw parameter specifies whether missing ratings are to be omitted or assigned an average rating.

This all works, but the calling expression shown above is error prone and obtuse. If I want to add a feature, I have to be careful where and how I add it or the expression will not update correctly.

Option #2

An alternative is to name the ranges. If I assign:
  • D5:D9 = BestRange
  • E5:E9 = WorstRange
  • F5:F9 = WtRange
  • H5:H9 = ProdARange
  • I5:I9 = ProdBRange
  • J5:J9 = ProdCRange
then I can code:
Code:
=wtdrtg(ProdARange,BestRange,WorstRange,WtRange)
=wtdrtg(ProdBRange,BestRange,WorstRange,WtRange)
=wtdrtg(ProdCRange,BestRange,WorstRange,WtRange)
This is far superior to the first option in readability, but it has exactly the same problem with adding features.

Option #3

A third option is to assign names to the cells just above and below each range and then use the offset function to address and actual range. To make this work, I would assign these names:
  • D4 = BestTop
  • D10 = BestBot
  • E4 = WorstTop
  • E10 = WorstBot
  • ... etc ...
This leads to this abomination:
Code:
=wtdrtg(OFFSET(ProdATop,1,0):OFFSET(ProdABot,-1,0),OFFSET(BestTop,1,0):OFFSET(BestBot,-1,0),OFFSET(WorstTop,1,0):OFFSET(WorstBot,-1,0),OFFSET(WtTop,1,0):OFFSET(
This avoids all of the problems with adding features, but it is a nightmare to code -- highly error prone and unreadable.

Option #4

A fourth option is to expand the named ranges in option #2 to include the cells immediately above and below the actual range. Then change the UDF code to ignore these two cells.

I think that's the path I will take unless someone can suggest something better.

Any suggestions?

Thanks
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
So a little hard to tell based on the info you gave how you will use this going forward. If you will have say a template, where the user (or you) will always put the data in the same place (the top starts in the same cell), then you could create dynamic named ranges to hold the data, right? So instead of using Top and Bottom values, use a classic named range where Excel counts the data and returns the entire range for you based on the top cell in the range (Features, Best, Worst, Weight). So say you have the following named ranges:

rngFeaturesHead: C4
rngBestHead: D4
rngWorstHead: E4
rngWeightHead: F4

You could then create dynamic ranges for these, along the lines of:

rngFeatures: =OFFSET(rngFeaturesHead,1,,COUNTA(Sheet1!$C$5:$C$100))
rngBest: =OFFSET(rngBestHead,1,,COUNTA(Sheet1!$C$5:$C$100))
rngWorst: etc.
rngWeight etc.

It isn't super elegant with the C5:C100 (you can use whatever you think the max will be instead of 100), but the user wouldn't see it.

Anyway, my 2 cents with the information as I understand it. Have fun!
 
Upvote 0
I think a UDF is overkill ...

A​
B​
C​
D​
E​
F​
G​
H​
I​
1​
7.98​
9.15​
6.37​
E1: =10 * SUMPRODUCT((tbl myCol - Worst) / (Best - Worst) * Wgt) / SUM(Wgt)
2​
Features
Best
Worst
Wgt
Car A
Car B
Car C
3​
Gas Mileage
50​
20​
8​
35​
43​
28​
4​
Price
25000​
60000​
7​
27000​
31000​
46000​
5​
Warranty
5​
2​
4​
3​
5​
3​
6​
Reliability Index
0​
200​
10​
12​
3​
21​
7​
GPS
1​
0​
9​
1​
1​
1​
8​
9​
tbl RefersTo: =INDEX(Sheet1!$B:$B, ROW(Sheet1!$B$2)+1):INDEX(Sheet1!$1:$1048576, MATCH("zzz", Sheet1!$A:$A), MATCH("zzz", Sheet1!$2:$2))
10​
Best RefersTo: =INDEX(tbl, 0, 1)
11​
Worst RefersTo: =INDEX(tbl, 0, 2)
12​
Wgt RefersTo: =INDEX(tbl, 0, 3)
13​
myCol RefersToR1C1: =C

Extend the table as desired.
 
Last edited:
Upvote 0
So a little hard to tell based on the info you gave how you will use this going forward. If you will have say a template, where the user (or you) will always put the data in the same place (the top starts in the same cell), then you could create dynamic named ranges to hold the data, right? So instead of using Top and Bottom values, use a classic named range where Excel counts the data and returns the entire range for you based on the top cell in the range (Features, Best, Worst, Weight). So say you have the following named ranges:

rngFeaturesHead: C4
rngBestHead: D4
rngWorstHead: E4
rngWeightHead: F4

You could then create dynamic ranges for these, along the lines of:

rngFeatures: =OFFSET(rngFeaturesHead,1,,COUNTA(Sheet1!$C$5:$C$100))
rngBest: =OFFSET(rngBestHead,1,,COUNTA(Sheet1!$C$5:$C$100))
rngWorst: etc.
rngWeight etc.

It isn't super elegant with the C5:C100 (you can use whatever you think the max will be instead of 100), but the user wouldn't see it.

Anyway, my 2 cents with the information as I understand it. Have fun!

I did not intend a template, per se. The user (me) would set up the Best, Worst, & Weight ranges anyway they like. They can be rows or columns. They they set up 1 or more ranges for the products.

Your CountA suggestion is clever. I wouldn't have thought of that.
 
Upvote 0
I think a UDF is overkill ...

Tomatoes, tomahtoes, no? :confused:

My tomatoes: I am trying to get to the simplest expression possible.

Your tomahtoes (I think): Avoid UDFs wherever possible.

Regardless, I do need the UDF. It handles a lot of exception conditions and special cases.

  1. If a product rating is omitted, it will either skip it or assign it an average value depending on an optional parameter.
  2. If a product rating is out of range, it will either assign it a Best/Worst value or continue to scale it outside the range based on another optional parameter.
And I have a couple of other possible options that I might add. I really need the logic of a UDF.
 
Upvote 0
I really need the logic of a UDF.

Code:
=JMUDF(tbl As Range, Best As Range, Worst As Range, Wgt As Range)

There's enough information there to figure out the table orientation and the ratings associated with the calling cell.
 
Last edited:
Upvote 0
Tomatoes, tomahtoes, no? :confused:

My tomatoes: I am trying to get to the simplest expression possible.

Your tomahtoes (I think): Avoid UDFs wherever possible.

Hi Jennifer,

I didn't interpret shg's to be suggesting "Avoid UDFs wherever possible."

Russel noted that (from the OP alone) it was hard to tell how you were going to use this. Critical factors include whether this needs to be reused in different configurations, and whether this is for your sole use.

Your response in #5, addresses those factors...
I did not intend a template, per se. The user (me) would set up the Best, Worst, & Weight ranges anyway they like. They can be rows or columns. They set up 1 or more ranges for the products.

If this is just for your use (implying you'll have control and understanding of the allowable configurations), you could consider simplifying to:

=wtdrtg(ProdARange,WtTable)

WtTable can be the entire Ratings and Weight range including labels ("Best","Worst","Wgt"). In your OP B2:D7.

Your UDF can use those labels to determine if the orientation is by rows or by columns, and you would have the flexibility to change the order of those rows/columns if needed.
 
Upvote 0
As an aside, for linear interpolation of an interval, IMO it is more intuitive to put Worst to the left of Best.
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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