- (operator)

Syntax 1

expression1 expression2

Syntax 2

expression

Description

Returns the difference between expression1 and expression2 or, in the second syntax, returns the negation of expression.

Comments

Syntax 1

The type of the result is the same as that of the most precise expression, with the following exceptions:

 

If one
expression is

and the other
expression is

then the type
result is

 

Long

Single

Double

 

Boolean

Boolean

Integer

 

A runtime error is generated if the result overflows its legal range.

When either or both expressions are Variant, then the following additional rules apply:

If expression1 is Null and expression2 is Boolean, then the result is Empty. Otherwise, if either expression is Null, then the result is Null.

Empty is treated as an Integer of value 0.

If the type of the result is an Integer variant that overflows, then the result is a Long variant.

If the type of the result is a Long, Single, or Date variant that overflows, then the result is a Double variant.

 

Syntax 2

If expression is numeric, then the type of the result is the same type as expression, with the following exception:

If expression is Boolean, then the result is Integer.

 

In 2's compliment arithmetic, unary minus may result in an overflow with Integer and Long variables when the value of expression is the largest negative number representable for that data type. For example, the following generates an overflow error:

Sub Main() 
  Dim a As Integer
  a = -32768
  a = -a        '<-- Generates overflow here.
End Sub

When negating variants, overflow will never occur because the result will be automatically promoted: integers to longs and longs to doubles.

Example

This example assigns values to two numeric variables and their difference to a third variable, then displays the result.

Sub Main()
  i% = 100
  j# = 22.55
  k# = i% - j#
  MsgBox "The difference is: " & k#
End Sub

See Also

Operator Precedence (topic).

More information

Symbols