Concatenation Operators in VBA

VBA concatenation operators - symbols that instruct the compiler to join two String operands together - are explored herein.

In this article:

3 minutes read

VBA Operators

VBA operators are special tokens that instruct the compiler to “operate” on values or program identifiers. They are also referred to as program symbols or code elements. Also, the program identifiers they operate on (e.g., addition) must be capable of holding values.

You may have heard of the term operand. These are the values (i.e., literals) or identifiers (e.g., variables and constants) that operators act on. Based on their number of operands, operators are either unary or binary.

Unary VBA operators are operators that only act on a single operand at a time. Binary VBA operators are those that only act on two operands at a time.

VBA supports the following categories of operators:

This article takes an in-depth look at the VBA concatenation operators.

VBA Operator Precedence

Often, an operation involves more than one operator, sometimes of different categories. So, it is worthwhile paying special attention to the order of precedence of the VBA operators. Otherwise, VBA’s default operator precedence applies.

The default precedence is as follows: arithmetic first, then concatenation, followed by comparison, then logical, and finally assignment operators.

Moreover, there is a pre-set precedence for operators within each category. How do you override this default order and remove any ambiguity? Easy, set the preferred precedence with open and close parentheses, ( ).

Concatenation Operators

Concatenation operators join two String operands together. The result is a single expression, with the operand after the operator appended to the operand before it. The table below summarizes the different VBA concatenation operators.

OperatorDescriptionReturn Data TypePrecedence
&String Concatenation (binary) operator, i.e. ampersand (&), appends two String or String Variant operands into a String expression.String, if both operands are String;

String Variant, if at least one operand is not a String;

Null, if both operands are Null.
1
+String Concatenation (binary) operator, i.e. plus (+), appends two Strings, two String Variants, or one String and any Variant (except Null or numeric types) operands into a String expression.String, if both operands are Strings;

String Variant, if both operands are String Variants;

String Variant, if one operand is String and the other is any Variant (except Null or numeric types);

Null, if either operand is Null.
1

As shown in the table, both concatenation operators have the same precedence. So, when they both appear together in an expression, evaluation proceeds in the order of appearance from left to right.

Also, enclosing a part of an expression in parentheses causes its evaluation ahead of other parts. Moreover, note that the arithmetic plus (+) operator has precedence over concatenation plus (+).

Concatenation Operators: Ampersand (&)

The sample code below shows the ampersand (&) operator’s use in joining Strings.

Sample code illustrating the ampersand (&) concatenation operator’s usage. Non-String operands are converted to String Variants, while Null and Empty expressions are treated as zero-length strings (i.e., “”).
Sample code illustrating the Ampersand (&) operator’s usage – non-string operands are converted to String Variants, while Null and Empty expressions are treated as zero-length strings (i.e., “”).

Concatenation Operator: Plus (+)

The sample code below illustrates the plus (+) operator’s use in joining Strings.

Sample code illustrating the Plus (+) concatenation operator’s usage. If one operand is a String and the other is numeric, a “Type Mismatch” error is thrown.
Sample code illustrating the Plus (+) operator’s usage – If one operand is a String and the other is numeric, a “Type Mismatch” error is thrown.
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Keep Learning

Related Articles

Keep Learning

Related Articles