Table of contents
TOC
Collapse the table of content
Expand the table of content

+ Operator

office 365 dev account|Last Updated: 6/12/2017
|
1 Contributor

Used to sum two numbers. Syntaxresult=expression1 + expression2. The + operator syntax has these parts:

PartDescription
resultRequired; any numeric variable.
expression1Required; any expression.
expression2Required; any expression.

Remarks When you use the + operator, you may not be able to determine whether addition or string concatenation will occur. Use the &; operator for concatenation to eliminate ambiguity and provide self-documenting code. If at least one expression is not a Variant, the following rules apply:

IfThen
Both expressions are numeric data types (Byte, Boolean, Integer, Long, Single, Double, Date, Currency, or Decimal)Add.
Both expressions are StringConcatenate.
One expression is a numeric data type and the other is any Variant except NullAdd.
One expression is a String and the other is any Variant except NullConcatenate.
One expression is an EmptyVariantReturn the remaining expression unchanged as result.
One expression is a numeric data type and the other is a StringA Type mismatcherror occurs.
Either expression is Nullresult is Null.

If both expressions are Variant expressions, the following rules apply:

IfThen
Both Variant expressions are numericAdd.
Both Variant expressions are stringsConcatenate.
One Variant expression is numeric and the other is a stringAdd.

For simple arithmetic addition involving only expressions of numeric data types, the data type of result is usually the same as that of the most precise expression. The order of precision, from least to most precise, is Byte, Integer, Long, Single, Double, Currency, and Decimal. The following are exceptions to this order:

IfThen result is
A Single and a Long are added,a Double.
The data type of result is a Long, Single, or Date variant that overflows its legal range,converted to a Double variant.
The data type of result is a Byte variant that overflows its legal range,converted to an Integer variant.
The data type of result is an Integer variant that overflows its legal range,converted to a Long variant.
A Date is added to any data type,a Date.

If one or both expressions are Null expressions, result is Null. If both expressions are Empty, result is an Integer. However, if only one expression is Empty, the other expression is returned unchanged as result.

Note The order of precision used by addition and subtraction is not the same as the order of precision used by multiplication.

Example

This example uses the + operator to sum numbers. The + operator can also be used to concatenate strings. However, to eliminate ambiguity, you should use the &; operator instead. If the components of an expression created with the + operator include both strings and numerics, the arithmetic result is assigned. If the components are exclusively strings, the strings are concatenated.

Dim MyNumber, Var1, Var2
MyNumber = 2 + 2    ' Returns 4.
MyNumber = 4257.04 + 98112    ' Returns 102369.04.

Var1 = "34": Var2 = 6    ' Initialize mixed variables.
MyNumber = Var1 + Var2    ' Returns 40.

Var1 = "34": Var2 = "6"    ' Initialize variables with strings.
MyNumber = Var1 + Var2    ' Returns "346" (string concatenation).
© 2018 Microsoft