11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

682<br />

Part VI: Developing Custom Worksheet Functions<br />

A more general function, one that accepts array constants as well as ranges, is shown here:<br />

Function DRAWONE2(rng As Variant) As Variant<br />

‘ Chooses one value at random from an array<br />

Dim ArrayLen As Long<br />

If TypeName(rng) = “Range” Then<br />

DRAWONE2 = rng(Int((rng.Count) * Rnd + 1)).Value<br />

Else<br />

ArrayLen = UBound(rng) – LBound(rng) + 1<br />

DRAWONE2 = rng(Int(ArrayLen * Rnd + 1))<br />

End If<br />

End Function<br />

This function uses the VBA built-in TypeName function to determine whether the argument<br />

passed is a Range. If not, it’s assumed to be an array. Following is a formula that uses the<br />

DRAWONE2 function. This formula returns a text string that corresponds to a suit in a deck of<br />

cards:<br />

=DRAWONE2({“Clubs”,”Hearts”,”Diamonds”,”Spades”})<br />

Following is a formula that has the same result, written using Excel’s built-in functions:<br />

=CHOOSE(RANDBETWEEN(1,3),”Clubs”,”Hearts”,”Diamonds”,”Spades”)<br />

I present two additional functions that deal with randomization later in this chapter (see the<br />

“Advanced Function Techniques” section).<br />

Calculating Sales Commissions<br />

Sales managers often need to calculate the commissions earned by their sales forces. The calculations<br />

in the function example presented here are based on a sliding scale: Employees who sell<br />

more earn a higher commission rate (see Table 25-1). For example, a salesperson with sales<br />

between $10,000 and $19,999 qualifies for a commission rate of 10.5 percent.<br />

Table 25-1: Commission Rates for Monthly Sales<br />

Monthly Sales Commission Rate<br />

Less than $10,000 8.0%<br />

$10,000 to $19,999 10.5%<br />

$20,000 to $39,999 12.0%<br />

$40,000 or more 14.0%

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

Saved successfully!

Ooh no, something went wrong!