VBA - Concatenation Operators


Advertisements

Following Concatenation operators are supported by VBA.

Assume variable A holds 5 and variable B holds 10 then −

Operator Description Example
+ Adds two Values as Variable. Values are Numeric A + B will give 15
& Concatenates two Values A & B will give 510

Example

Try the following example to understand the Concatenation operator available in VBScript −

Private Sub Constant_demo_Click()
   Dim a as Integer : a = 5
   Dim b as Integer : b = 10
   Dim c as Integer

   c = a + b  
   msgbox ("Concatenated value:1 is " &c) 'Numeric addition 
   
   c = a & b 
   msgbox ("Concatenated value:2 is " &c) 'Concatenate two numbers 
End Sub

Try the following example to understand all the Logical operators available in VBA by creating a button and adding the following function.

Concatenated value:1 is 15

Concatenated value:2 is 510

Concatenation can also be used for concatenating two strings. Assume variable A = "Microsoft" and variable B = "VBScript" then −

Operator Description Example
+ Concatenates two Values A + B will give MicrosoftVBScript
& Concatenates two Values A & B will give MicrosoftVBScript

Example

Try the following example to understand all the Logical operators available in VBA by creating a button and adding the following function.

Private Sub Constant_demo_Click()
   Dim a as String : a = "Microsoft"
   Dim b as String : b = "VBScript"
   Dim c as String

   c = a + b 
   msgbox("Concatenated value:1 is " &c) 'addition of two Strings
   
   c = a & b 
   msgbox("Concatenated value:2 is " &c) 'Concatenate two String
End Sub

When you save it as .html and execute it in the Internet Explorer, then the above script will produce the following result.

Concatenated value:1 is MicrosoftVBScript

Concatenated value:2 is MicrosoftVBScript
vba_operators.htm
Advertisements