30.04.2017 Views

4523756273

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 20: Creating Worksheet Functions — and Living to Tell about It<br />

339<br />

Select Case Sales<br />

Case 0 To 9999.99: Commission2=Sales*Tier1<br />

Case 10000 To 19999.99: Commission2=Sales*Tier2<br />

Case 20000 To 39999.99: Commission2=Sales*Tier3<br />

Case Is>=40000: Commission2=Sales*Tier4<br />

End Select<br />

Commission2=Commission2+(Commission2*Years/100)<br />

Commission2=Round(Commission2, 2)<br />

End Function<br />

I simply added the second argument (Years) to the Function statement and<br />

included an additional computation that adjusts the commission before exiting<br />

the function. This additional computation multiplies the original commission<br />

by the number of years in services, divides by 100, and then adds the<br />

result to the original computation.<br />

Here’s an example of how you can write a formula by using this function. (It<br />

assumes that the sales amount is in cell A1; cell B1 specifies the number of<br />

years the salesperson has worked.)<br />

=Commission2(A1,B1)<br />

A function with a range argument<br />

Using a worksheet range as an argument is not at all tricky; Excel takes care<br />

of the behind-the-scenes details.<br />

Assume that you want to calculate the average of the five largest values in<br />

a range named Data. Excel doesn’t have a function that can do this, so you<br />

would probably write a formula:<br />

=(LARGE(Data,1)+LARGE(Data,2)+LARGE(Data,3)+<br />

LARGE(Data,4)+LARGE(Data,5))/5<br />

This formula uses Excel’s LARGE function, which returns the nth largest<br />

value in a range. The formula adds the five largest values in the range named<br />

Data and then divides the result by 5. The formula works fine, but it’s rather<br />

unwieldy. And what if you decide that you need to compute the average of<br />

the top six values? You would need to rewrite the formula — and make sure<br />

that you update all copies of the formula.<br />

Wouldn’t this be easier if Excel had a function named TopAvg? Then you<br />

could compute the average by using the following (nonexistent) function:<br />

=TopAvg(Data,5)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!