+ Operator

Used to sum two numbers.

Syntax

result = operand1 + operand2

Remarks

Although you can also use the + operator to concatenate two character strings, you should use the & operator for concatenation to eliminate ambiguity and provide self-documenting code.

When you use the + operator, you may not be able to determine whether addition or string concatenation is to occur.  If at least one operand is not a Variant, the following rules apply:

 If Then

 Both expressions are numeric data types (Integer, Long, Single, Double, or Currency) Add. Both expressions are Strings Concatenate. One expression is a numeric data type and the other is a Variant (other than a Null) Add. One expression is a String and the other is a Variant (other than a Null) Concatenate. One expression is a Variant containing Empty Return the remaining operand unchanged as result. One expression is a numeric data type and the other is a String A Type mismatch error occurs.

If either operand is a Null (VarType 1), result is a Null.

If both operands are Variant expressions, the VarType of the operands determines the behavior of the + operator in the following way:

 If Then

 Both Variant expressions are of VarType 2-7 (numeric data types) Add. Both Variant expressions are of VarType 8 (String) Concatenate. One Variant expression is of VarType 2-7 (a numeric data type) and the other is of VarType 8 (String) Add.

For simple arithmetic addition involving only operands of numeric data types, the data type 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, Double, Currency.  The following are exceptions to this order: When a Single and a Long are added together, the data type of result is converted to a Double. When the data type of result is a Variant of VarType 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 Null expressions, result is a Null.  If both operands are Empty, result is an Integer.  However, if only one operand is Empty, the other operand is returned unchanged as result.

Arithmetic Operators

Comparison Operators

Concatenation Operators

Logical Operators

Operator Precedence

Other Operators

VarType Function

+ Operator Example 1

The example adds a user-supplied number to itself 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 = N + N                              ' Add numbers.

MsgBox N & " plus " & N & " is " & (N + N)  ' Display result.

End Sub

+ Operator Example 2

The example uses the + operator to concatenate strings.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

Sub Form_Click ()

MsgBox "Microsoft " + "Visual " + "Basic"      ' Display result.

End Sub