- Operator

See Also7U640              Syntax 1 Example41XB0FQ>Low   Syntax 2 Example41XB0FR>Low

Used to find the difference between two numbers or to indicate the negative value of an operand.

Syntax 1

result = operand1 - operand2

Syntax 2

-number

Remarks

In Syntax 1, the - operator is the arithmetic subtraction operator used to find the difference between two numbers.  The operands can be any numeric expression71RISN.

The data type3GYXY7 of result is usually the same as that of the most precise operand.  The order of precision, from least to most precise, is Integer, Long, Single, Currency, Double.  The following are exceptions to this order:

         When subtraction involves a Single and a Long, the data type of result is converted to a Double.

         When the data type of result is a Variant of VarType7A68ZTZ 3 (Long), VarType 4 (Single), or  VarType 7 (Date) that overflows its legal range, result is converted to a Variant of VarType 5 (Double).

         When the data type of result is a Variant of VarType 2 (Integer) that overflows its legal range, result is converted to a Variant of VarType 3 (Long).

 

If one or both operands are Null1DDW7C0 expressions, result is a Null.  If an operand is Empty1L2JEZ4 (VarType 0), it is treated as if it were 0.

In Syntax 2, the - operator is used as the unary negation operator to indicate the negative value of an operand.  As with Syntax 1, the operand can be any numeric constant, variable, expression, or any function that returns a number.


See Also

Arithmetic OperatorsSH0K1B

Comparison OperatorsNRJZ1K

Concatenation Operators2MYTW1K

Logical Operators1CQVUTN

Operator Precedence23082A9

Other Operators2HA580P

VarType FunctionXRZH1Y


- Operator Syntax 1 Example

The example subtracts a user-supplied number from 1000 and displays the result  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

 

Sub Form_Click ()

   Dim N As Double, Result As Double           ' Declare variables.

   N = InputBox ("Enter a number.")            ' Get a number.

   Result = 1000 -N

   MsgBox "1000 minus " & N & " is " & Result  ' Display result.

End Sub


- Operator Syntax 2 Example

The example uses the - operator to indicate a negative value of a number.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

 

Sub Form_Click ()

   Dim N As Double, MinusN As Double           ' Declare variables.

   N = InputBox("Enter a number")              ' Get a number.

   MinusN = -N                                 ' Use negation operator.

   MsgBox "Negative " & N & " is " & MinusN    ' Display result.

End Sub