## Calculations in VBA

Some of the examples looked at to date have involved mathematical calculations. Many of the symbols that are used in everyday arithmetic are the same in VBA programs. For example, the (+) sign for addition and the (-) minus sign for subtraction. However, because of keyboard ambiguities and difficulties, there are some arithmetical symbols that are different. For instance, the arithmetical multiplication symbol (x) is denoted by the asterisk symbol (*) on a computer. The reason for not using the x symbol is to prevent ambiguity with the alphabetical meaning of x. Also, exponentiation is denoted by a superscript in everyday arithmetic, but this is difficult to implement on a computer keyboard and therefore the symbol for exponentiation is the caret (A).

Table 3.3 displays the VBA arithmetical operators and their everyday equivalents. Note the two additional operators that are available in VBA programs, namely integer division (\) and modulo division (Mod).

When integer division is used, the integer portion of the result is returned only, e.g. 9 \ 2 = 4 (the fractional part of the quotient is discarded).

When modulo division is used, the integer portion of the result is discarded and the remainder is returned, e.g. 9 Mod 2 =1 since the remainder part is 1.

 Operator Math symbol VBA symbol Math example VBA example Addition + + 6+7 6+7 Subtraction - - 7-4 7-4 Multiplication X * 7x4 7*4 Division -5- / 7-^4 7/4 Exponentiation A 74 7A4 Integer division \ 7\2 (=3) Modulo division Mod 7 Mod 2 (= 1)

Table 3.3 VBA mathematical operators

Table 3.3 VBA mathematical operators

0 0