Excel VBA Concatenate Strings
Two or more substrings joining together to form a sentence or new strings are known as concatenation. We have seen concatenation in the worksheet either by using the ampersand operator or the addition operator. Even there is a separate function for concatenation in worksheet functions. But in VBA it is not that similar, in VBA there is no inbuilt function for concatenation of strings also neither can we use the worksheet functions for the same. The only method to concatenate strings in excel VBA is by using the & and + operator.
How to Concatenate Strings in VBA?
Below are the different examples to use the Concatenate Strings in Excel VBA.
VBA Concatenate Strings – Example #1
First, let us begin with the most basic concept of using the & or known as ampersand keystroke used to concatenation in VBA. For this, follow the steps below:
Step 1: Insert a new module inside Visual Basic Editor (VBE). Click on Insert tab > select Module.
Step 2: Now we can begin with our subprocedure to show how to concatenate strings in VBA.
Code:
Sub Example1() End Sub
Step 3: Now we need two string variables declared to show how concatenation is done.
Code:
Sub Example1() Dim Str1, Str2 As String End Sub
Step 4: Let us assign some random values to the variables.
Code:
Sub Example1() Dim Str1, Str2 As String Str1 = "Good" Str2 = "Boy" End Sub
Step 5: Now we can use the & or ampersand operator to concatenate both variables.
Code:
Sub Example1() Dim Str1, Str2 As String Str1 = "Good" Str2 = "Boy" MsgBox Str1 & Str2 End Sub
Step 6: Now when we execute the above code we can see the following result.
The strings are concatenated together but there is no space available because we didn’t provide it. So this is another lesson that we need to concatenate space too as space is also a character.
VBA Concatenate Strings – Example #2
In the above example we have seen how we concatenated strings also we know that we can use + or addition operator to concatenate strings, can we use concatenation on numbers using the addition operator? Let’s find out. For this, follow the steps below:
Step 1: In the same module let us start another subprocedure as shown below.
Code:
Sub Example2() End Sub
Step 2: Declare two variables as an integer for the integer values.
Code:
Sub Example2() Dim int1, int2 As Integer End Sub
Step 3: Then assign some values to these integer variables.
Code:
Sub Example2() Dim int1, int2 As Integer int1 = 21 int2 = 23 End Sub
Step 4: Now let us use the addition operator for the concatenation.
Code:
Sub Example2() Dim int1, int2 As Integer int1 = 21 int2 = 23 MsgBox int1 + int2 End Sub
Step 5: If we execute the now we will not get the desired result.
Step 6: So, we cannot use the addition operator for concatenation when we use integers, we have to use the ampersand operator and also we need to provide a space.
Code:
Sub Example2() Dim int1, int2 As Integer int1 = 21 int2 = 23 MsgBox int1 & " " & int2 End Sub
Step 7: Now, let us execute the Code by pressing the F5 key or by clicking on the Play Button.
VBA Concatenate Strings – Example #3
So, we cannot use addition operators for integers but we can use them if we treat integer values as string. In this example, we will use integer values as strings. For this, follow the steps below:
Step 1: In the same module let us start with the third example.
Sub Example3() End Sub
Step 2: Now declare two variables as String as shown below.
Code:
Sub Example3() Dim Str1, Str2 As String End Sub
Step 3: Now, assign integer values to the string variables in double quotation marks.
Code:
Sub Example3() Dim Str1, Str2 As String Str1 = "21" Str2 = "23" End Sub
Step 4: Now using the Msgbox function we will display both using the ampersand operator.
Code:
Sub Example3() Dim Str1, Str2 As String Str1 = "21" Str2 = "23" MsgBox Str1 & " " & Str2 End Sub
Step 5: Run the code by pressing the F5 key or by clicking on the Play Button.
VBA Concatenate Strings – Example #4
Now let us note that there may be some circumstances that we need to use the values from worksheets to concatenate. We have two separate values in the sheet as follows. For this, follow the steps below:
Step 1: So in the same module let us start another subprocedure for example 4.
Code:
Sub Example4() End Sub
Step 2: Now declare three variables as Strings.
Code:
Sub Example4() Dim Str1, Str2, Str3 As String End Sub
Step 3: In the first three variables let us store the values from the worksheets.
Code:
Sub Example4() Dim Str1, Str2, Str3 As String Str1 = Range("A1").Value Str2 = Range("B1").Value End Sub
Step 4: Now we will use the addition operator for concatenation.
Code:
Sub Example4() Dim Str1, Str2, Str3 As String Str1 = Range("A1").Value Str2 = Range("B1").Value Str3 = Str1 + Str2 End Sub
Step 5: And we can put the new value in the other cell.
Code:
Sub Example4() Dim Str1, Str2, Str3 As String Str1 = Range("A1").Value Str2 = Range("B1").Value Str3 = Str1 + Str2 Range("C1").Value = Str3 End Sub
Step 6: When we execute the code we can see the result.
As explained above there is no inbuilt function for VBA to concatenate strings. Rather than the worksheet function, we use the & (ampersand) and + (addition) operators for it.
Things to Remember About VBA Concatenate Strings
There are few things which we need to remember about concatenate strings in VBA and they are as follows:
1. In the above four examples, we saw that there are two methods to concatenate strings in VBA.
Method 1: By using the ampersand (&) operator.
Method 2: By using the addition (+) operator.
2. We use ampersand and addition operator to concatenate strings in VBA.
3. There is no such function similar to the worksheet concatenate function in VBA.
4. Addition operators used on Integers will add the integers.
5. Space is also a character so between strings Space also needs to be concatenated.
Recommended Articles
This is a guide to VBA Concatenate Strings. Here we discuss How to Concatenate Strings in Excel VBA along with practical examples and downloadable excel template. You can also go through our other related articles to learn more –