## Listing The Correct Way to Calculate the Pre Tax Cost

Function PreTaxCost2(totalCost As Currency, taxRate As Single) As Currency

PreTaxCost2 = totalCost / (1 + taxRate) End Function

Figure 4.4

The revised function calculates the pre-tax cost correctly.

Figure 4.4

The revised function calculates the pre-tax cost correctly.

 Chapter04 _ S A B c D £ F G 1 Sales Tax Rate: 7% 1 2 3 Price Pre-Tax Cost = 4 H 0.65 1 \$9.95 5 6 7 S - > » With Precedence Control SheefWV] HI-

In general, you can use parentheses to control the order that VBA uses to calculate expressions. Terms inside parentheses are always calculated first; terms outside parentheses are calculated sequentially (according to the order of precedence). To gain even more control over your expressions, you can place parentheses inside one another; this is called nesting parentheses, and VBA always evaluates the innermost set of parentheses first. Here are a few sample expressions:

 Expression First Step Second Step Third Step Result 3A(15/5)*2-5 3A3*2-5 27*2-5 54-5 49 3A((15/5)*2-5) 3A(3*2-5) 3A(6-5) 3A1 3 3A(15/(5*2-5)) 3A(i5/(io-5)) 3A(15/5) 3A3 27

Notice that the order of precedence rules also hold within parentheses. For example, in the expression (5*2-5), the term 5*2 is calculated before 5 is subtracted.

Using parentheses to determine the order of calculations gives you full control over VBA expressions. This way, you can make sure that the answer given by an expression is the one

I you want.

0 0