A function with a range argument. Microsoft Excel VBA Programming
विज्ञापन
विज्ञापन
338
Part V: Putting It All Together
Notice that the four commission rates are declared as constants, rather than hard-coded. This makes it very easy to modify the function if the commission rates change.
After you define this function in a VBA module, you can use it in a worksheet formula. Entering the following formula into a cell produces a result of 3,000.
The amount of 25000 qualifies for a commission rate of 12 percent:
=Commission(25000)
Figure 20-1 shows a worksheet that uses the Commission function in formulas in column C.
Figure 20-1:
Using the
Commission function in a worksheet.
A function with two arguments
The next example builds on the preceding one. Imagine that the sales manager implements a new policy to reward long-term employees: The total commission paid increases by 1 percent for every year the salesperson has been with the company.
I modified the custom Commission function (defined in the preceding section) so that it takes two arguments, both of which are required arguments.
Call this new function Commission2:
Function Commission2(Sales, Years)
‘ Calculates sales commissions based on years in service
Const Tier1 As Double = 0.08
Const Tier2 As Double = 0.105
Const Tier3 As Double = 0.12
Const Tier4 As Double = 0.14
डाउनलोड
विज्ञापन