MOCKSTACKS
EN
Questions And Answers

More Tutorials









VBA Operators

Concatenation Operators


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.


Conclusion

In this page (written and validated by ) you learned about VBA Operators . What's Next? If you are interested in completing VBA tutorial, your next topic will be learning about: VBA Sorting.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.