Return to VBA Code Examples
In this Article
- Concatenate Strings
- Concatenate Cells
- Concatenate Variables
- Using the & Operator with Spaces
- Using the & Operator to Concatenate a Quotation Mark
- Putting Strings on a New line
We have already gone over an introduction to string functions in our VBA Strings and Substrings Functions tutorial. We are now going to look at how to concatenate text strings.
Concatenate Strings
You can use the & operator in VBA to join text strings.
MsgBox "Merge" & "Text"
Concatenate Cells
You can also concatenate cells together. Below, we have the text strings in A1 and B1:
The following code shows you how to join text strings from cell A1 and B1 using the & operator, in cell C1:
Range("C1").Value = Range("A1").Value & Range("B1").value
The result is:
Concatenate Variables
This is the full procedure to concatenate two cells together using string variables.
Sub ConcatenateStrings()
Dim StringOne as String
Dim StringTwo as String
StringOne = Range("A1").Value
StringTwo = Range("B1").Value
Range("C1").Value = StringOne & StringTwo
End Sub
Using the & Operator with Spaces
When you want to include spaces you use & in conjunction with ” “. The following code shows you how you would include spaces:
Sub ConcatenatingStringsWithSpaces()
Dim StringOne As String
Dim StringTwo As String
Dim StringThree As String
StringOne = "This is"
StringTwo = "the text"
StringThree = StringOne & " " & StringTwo
MsgBox StringThree
End Sub
The MessageBox result is:
Using the & Operator to Concatenate a Quotation Mark
Let’s say your text string contains a quotation mark, the following code shows you how to include a quotation mark within a text string:
Sub ConcatenatingAQuotationMark()
Dim StringOne As String
Dim StringTwo As String
Dim StringThree As String
StringOne = "This is the quotation mark"
StringTwo = """"
StringThree = StringOne & " " & StringTwo
MsgBox StringThree
End Sub
The result is:
Putting Strings on a New line
Let’s say you have five text strings, you can put each text string on a new line or paragraph, using either the vbNewLine, vbCrLf, vbCr or Chr Function. The following code shows you how to put each text string on a new line:
Sub PuttingEachTextStringOnANewLine()
Dim StringOne As String
Dim StringTwo As String
Dim StringThree As String
Dim StringFour As String
Dim StringFive As String
StringOne = "This is the first string"
StringTwo = "This is the second string"
StringThree = "This is the third string"
StringFour = "This is the fourth string"
StringFive = "This is the fifth string"
MsgBox StringOne & vbNewLine & StringTwo & vbCrLf & StringThree & vbCr & StringFour & Chr(13) & StringFive
End Sub
The result is:
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
This question comes from a comment under Range.Formula= in VBA throws a strange error.
I wrote that program by trial-and-error so I naturally tried +
to concatenate strings.
But is &
more correct than +
for concatenating strings?
Teamothy
1,9903 gold badges15 silver badges24 bronze badges
asked Nov 13, 2009 at 7:31
&
is always evaluated in a string context, while +
may not concatenate if one of the operands is no string:
"1" + "2" => "12"
"1" + 2 => 3
1 + "2" => 3
"a" + 2 => type mismatch
This is simply a subtle source of potential bugs and therefore should be avoided. &
always means «string concatenation», even if its arguments are non-strings:
"1" & "2" => "12"
"1" & 2 => "12"
1 & "2" => "12"
1 & 2 => "12"
"a" & 2 => "a2"
answered Nov 13, 2009 at 7:34
JoeyJoey
341k85 gold badges687 silver badges681 bronze badges
2
The main (very interesting) difference for me is that:
"string" & Null
-> "string"
while
"string" + Null
-> Null
But that’s probably more useful in database apps like Access.
answered Jun 5, 2018 at 15:48
iDevlopiDevlop
24.6k11 gold badges89 silver badges147 bronze badges
There is the concatenate function. For example
=CONCATENATE(E2,"-",F2)
But the & operator always concatenates strings. + often will work, but if there is a number in one of the cells, it won’t work as expected.
answered Nov 13, 2009 at 7:38
wallykwallyk
56.6k16 gold badges85 silver badges147 bronze badges
VBA in Excel stands for Visual Basic for Applications which is Microsoft’s programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. Concatenation means to join two or more data into a single data. There are various ways we can perform concatenation in Excel using built-in functions, operators, etc.
Some helpful links to get more insights about concatenate and using VBA in Excel :
- Record Macros in Excel
- CONCATENATE in Excel
- How to Create a Macro in Excel?
In this article, we are going to see about concatenate operators and how to use VBA to concatenate strings as well as numbers.
Implementation :
In the Microsoft Excel tabs, select the Developer Tab. Initially, the Developer Tab may not be available.
The Developer Tab can be enabled easily by a two-step process :
- Right-click on any of the existing tabs at the top of the Excel window.
- Now select Customize the Ribbon from the pop-down menu.
- In the Excel Options Box, check the box Developer to enable it and click on OK.
- Now, the Developer Tab is visible.
Now, we need to open the Visual Basic Editor. There are two ways :
- Go to Developer and directly click on the Visual Basic tab.
- Go to the Insert tab and then click on the Command button. Drag and insert the command button in any cell of the Excel sheet.
Now, double-click on this command button. This will open the Visual Basic Application Editor tab, and we can write the code. The benefit of this command button is that just by clicking on the button we can see the result of concatenation, and also we don’t need to make any extra Macro in Excel to write the code.
Another way is by right-clicking on the command button and then select View Code as shown below :
CONCATENATE Operators:
To concatenate operator mostly used in Excel is “&” for numbers as well as strings. But for strings, we can also use “+” operator to concatenate two or more strings.
The syntax to concatenate using formula is :
cell_number1 & cell_number2 ; without space in between cell_number1 & " " & cell_number2; with space in between cell_number1 & "Any_Symbol" & cell_number2 ; with any symbol in between cell_number(s) & "string text" ; concatenate cell values and strings "string_text" & cell_number(s) ; concatenate cell values and strings
CONCATENATE Two Numbers:
Example 1: Take any two numbers as input and concatenate into a single number.
The code to concatenate two numbers in Excel is :
Private Sub CommandButton1_Click() 'Inserting the number num1 and num2 Dim num1 As Integer: num1 = 10 Dim num2 As Integer: num2 = 50 'Declare a variable c to concatenate num1 and num2 Dim c As Integer 'Concatenate the numbers c = num1 & num2 MsgBox ("The result after concatenating two numbers are: " & c) End Sub
Run the above code in VBA and the output will be shown in the message box.
Output :
The result after concatenating two numbers are: 1050
You can also click on the command button and the same message will be displayed.
Example 2: Say, now we take two numbers from the cells of the Excel Sheet and store the result back in the Excel sheet. This time we are not going to use the command button.
Step 1: Open the VB editor from the Developer Tab.
Developer -> Visual Basic -> Tools -> Macros
Step 2: The editor is now ready where we can write the code and syntax for concatenation.
Now write the following code and run it.
Sub Concatenate_Numbers() 'Taking two variables to fetch the two numbers and to store Dim num1 As Integer Dim num2 As Integer 'Fetching the numbers from Excel cells num1 from A2 and num2 from B2 num1 = Range("A2").Value num2 = Range("B2").Value 'Find the concatenate and store in cell number C2 Range("C2").Value = num1 & num2 End Sub
CONCATENATED
Concatenate Two Or more strings:
We can use either “+” operator or “&” operator.
Example 1: Let’s concatenate the strings “Geeks”, “for”, “Geeks”.
Repeat Step 1 as discussed in the previous section to create a new VBA module of name “Concatenate_Strings”
Now write either of the following codes and run it to concatenate two or more strings.
Sub Concatenate_Strings() 'Taking three variables to fetch three strings and to store Dim str1 As String Dim str2 As String Dim str3 As String 'Fetching the strings from Excel cells str1 = Range("A2").Value str2 = Range("B2").Value str3 = Range("C2").Value 'Find the concatenate and store in cell number D2 Range("D2").Value = str1 + str2 + str3 End Sub
Sub Concatenate_Strings() 'Taking three variables to fetch three strings and to store Dim str1 As String Dim str2 As String Dim str3 As String 'Fetching the strings from Excel cells str1 = Range("A2").Value str2 = Range("B2").Value str3 = Range("C2").Value 'Find the concatenate and store in cell number D2 Range("D2").Value = str1 & str2 & str3 End Sub
CONCATENATED
Example 2: Let’s concatenate three strings and add in different lines and display them in a message box this time.
Create a new module and write the code. The keyword used for adding the string in the next line is vbNewLine.
The code is :
Sub Concatenate_DifferentLines() 'Taking three variables to store Dim str1 As String Dim str2 As String Dim str3 As String 'Initialize the strings str1 = "The best platform to learn any programming is" str2 = "none other than GeeksforGeeks" str3 = "Join today for best contents" 'Display the concatenated result in a message box MsgBox (str1 & vbNewLine & str2 & vbNewLine & str3) End Sub
Example 3: Let’s concatenate two strings with space in between by taking an example of the full name of a person which contains First Name and Last Name.
Create a new module Concatenate_Strings_Space and write the following code :
Sub Concatenate_Strings_Space() 'Taking two variables to fetch two strings and to store Dim fname As String Dim lname As String 'Fetching the strings from Excel cells fname = Range("A2").Value lname = Range("B2").Value 'Find the concatenate and store in cell number C2 Range("C2").Value = fname & " " & lname End Sub
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.
You can download this VBA Concatenate Strings Excel Template here – VBA Concatenate Strings Excel Template
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 –
- VBA SendKeys
- VBA On Error Goto
- VBA Msgbox Yes/No
- VBA SubString
Home / VBA / VBA Concatenate
To concatenate two strings using a VBA code, you need to use the ampersand. You can use an ampersand in between two strings to combine them and then assign that new value to a cell, variable, or message box. In the same way, you can concatenate more than two values as well.
Further, we will see a simple example to understand it.
Steps to use VBA to Concatenate
- First, enter the first string using double quotation marks.
- After that, type an ampersand.
- Next, enter the second text using double quotation marks.
- In the end, assign that value to a cell, or variable, or use a message box to see it.
Sub vba_concatenate()
Range("A1") = "Puneet " & "Gogia"
End Sub
You can also use a delimiter within two strings by simply adding a third ampersand. Consider the following code.
Range("A1") = "Puneet " & "-" & "Gogia"
In the above code, you have used a delimiter within two strings and joined them by simply using ampersands. So basically, whenever you need to join anything you have to use an ampersand within.
Concatenate using Variables
You can also store values in variables and then concatenate values from those two variables. Consider the following code.
In the above code, you have variables that are declared as variables and then you have assigned values to those variables. And in the end, we used an ampersand to combine all three variables and then assigned the result to cell A1.
Concatenate a Range using VBA
You can also concatenate values from a range of cells using a VBA. Consider the following macro.
Sub vba_concatenate()
Dim rng As Range
Dim i As String
Dim SourceRange As Range
Set SourceRange = Range("A1:A10")
For Each rng In SourceRange
i = i & rng & " "
Next rng
Range("B1").Value = Trim(i)
End Sub
In the above code, you have used the FOR NEXT (For Loops) to loop through the range that you want to concatenate.
So it goes to each cell of the range (A1:A10) stores that value in the I variable, and uses an ampersand to concatenate a value with each iteration. In the end, set the combined string to range B1.
And the following code concatenates the values from the selected range. All you need to do is to select a range and then run the code.
Dim rng As Range
Dim i As String
For Each rng In Selection
i = i & rng & " "
Next rng
Range("B1").Value = Trim(i)
Concatenate Entire Column or a Row
If you want to concatenate an entire column or a row, in that case, it’s better not to use the loop method. You can use the worksheet function “TextJoin” which can join an entire row or a column (consider the following code).
'join values from column A.
Dim myRange As Range
Dim myString As String
Range("B1") = WorksheetFunction.TextJoin(" ", True, Range("A:A"))
'join values from row 1.
Dim myRange As Range
Dim myString As String
Range("B1") = WorksheetFunction.TextJoin(" ", True, Range("1:1"))