Arithmetic Operators


These are the operators that do the arithmetical work, such as plus ( + ), minus ( ), multiply ( * ), and divide ( / ).

* Operator

This signifies the multiplication of two numbers .

 MsgBox 6 * 3 

This gives the answer 18.

The numbers can be any numeric expressions. The data type of the result is that of the most precise operand ranging from Integer (least precise), Long, Single, Double, Currency (most precise). See Chapter 2 for more details on these data types. If one operand is Null, then the result will be Null.

+ Operator

This adds two numbers or expressions together.

 MsgBox 4 + 2 

The answer will be 6.

This operator can both add numbers and concatenate strings. String concatenation can cause confusion, so it is best to use the & operator for this purpose because you cannot always determine if string concatenation will occur with +. See the example at the end of this section showing how string concatenation can be affected by use of the + operator.

The numbers can be any numeric expressions. The data type of the result is that of the most precise operand ranging from Integer (least precise), Long, Single, Double, Currency (most precise). If one operand is Null, then the result will be Null.

Here are some general rules of addition and concatenation:

  • Add if both operands are numeric.

  • Concatenate if both operands are strings.

  • Add if one operand is numeric and the other is a variant (not Null).

  • Concatenate if one operand is a string and the other is a variant (not Null).

A Type Mismatch error occurs if one operand is numeric and the other is string, as shown here:

 MsgBox 1 + " Richard" 

Note this does not happen if you use the & operator to concatenate, as shown here:

 MsgBox 1 & " Richard" 

‚ Operator

This subtracts one number from another or shows a negative value. The following will give an answer of 2:

 MsgBox 6 - 4 

The following will display ‚ 5:

 MsgBox -5 

The numbers can be any numeric expressions. The data type of the result is that of the most precise operand ranging from Integer (least precise), Long, Single, Double, Currency (most precise). If one operand is Null, then the result will be Null.

/ Operator

This divides two numbers and returns a floating point result.

 MsgBox 6 / 3 

The result is 2. If there were a remainder, it would be displayed as decimal places.

The numbers can be any numeric expressions. The data type of the result is that of the most precise operand ranging from Integer (least precise), Long, Single, Double, Currency (most precise). If one operand is Null, then the result will be Null.

\ Operator

This divides two numbers and returns an integer result.

 Msgbox 6 \ 4 

The answer is 1.

The numbers can be any numeric expressions. The data type of the result is Integer or Long. If one operand is Null, then the result will be Null.

^ Operator

This raises a number to the power of an exponent.

 MsgBox 2 ^ 3 

The answer is 8 (2 to the power of 3).

The operands can be any numeric expression. However, the number operand can be negative only if the exponent (power) is an integer.

Mod Operator

This divides two numbers and returns only the remainder.

 MsgBox 6 Mod 4 

This returns 2, which is the remainder of 6 divided by 4.

This is often used when testing to see if a number is odd or even. If the modulus is True (nonzero) when divided by two, then the number is odd.




Excel VBA Macro Programming
Excel VBA Macro Programming
ISBN: 0072231440
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net