VBA supports 2 different concatenation operators, +
and &
and both perform the exact same function when used with String
types - the right-hand String
is appended to the end of the left-hand String
.
If the &
operator is used with a variable type other than a String
, it is implicitly cast to a String
before being concatenated.
Note that the +
concatenation operator is an overload of the +
addition operator. The behavior of +
is determined by the variable types of the operands and precedence of operator types. If both operands are typed as a String
or Variant
with a sub-type of String
, they are concatenated:
Public Sub Example()
Dim left As String
Dim right As String
left = "5"
right = "5"
Debug.Print left + right 'Prints "55"
End Sub
If either side is a numeric type and the other side is a String
that can be coerced into a number, the type precedence of mathematical operators causes the operator to be treated as the addition operator and the numeric values are added:
Public Sub Example()
Dim left As Variant
Dim right As String
left = 5
right = "5"
Debug.Print left + right 'Prints 10
End Sub
This behavior can lead to subtle, hard to debug errors - especially if Variant
types are being used, so only the &
operator should typically be used for concatenation.