Конкатенация строк в excel на vba

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"

vba-concatenate

Concatenate Cells

You can also concatenate cells together. Below, we have the text strings in A1 and B1:

Concatenate Text Strings in VBA

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:

Using The Concatenate Operator in VBA to join Text Strings

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:

Concatenating Text in VBA with Spaces

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:

Using the Concatenate Operator to Concatenate a Quotation Mark

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:

Using vbNewLine to Put Text on its own line in VBA

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!
vba save as

Learn More!

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 :

  1. Record Macros in Excel
  2. CONCATENATE in Excel
  3. 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

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's user avatar

Teamothy

1,9903 gold badges15 silver badges24 bronze badges

asked Nov 13, 2009 at 7:31

ilya n.'s user avatar

& 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

Joey's user avatar

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

iDevlop's user avatar

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.

Community's user avatar

answered Nov 13, 2009 at 7:38

wallyk's user avatar

wallykwallyk

56.6k16 gold badges85 silver badges147 bronze badges

VBA Concatenate Strings

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.

Insert Module

Step 2: Now we can begin with our subprocedure to show how to concatenate strings in VBA.

Code:

Sub Example1()

End Sub

VBA Concatenate Strings Example1-1

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

VBA Concatenate Strings Example1-2

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

Random Values Example1-3

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

VBA Concatenate Strings Example1-4

Step 6: Now when we execute the above code we can see the following result.

VBA Concatenate Strings Example1-5

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

VBA Concatenate Strings Example2-1

Step 2: Declare two variables as an integer for the integer values.

Code:

Sub Example2()

Dim int1, int2 As Integer

End Sub

Declare Two Variables Example2-2

Step 3: Then assign some values to these integer variables.

Code:

Sub Example2()

Dim int1, int2 As Integer
int1 = 21
int2 = 23

End Sub

VBA Concatenate Strings Example2-3

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

Addition Operator Example2-4

Step 5: If we execute the now we will not get the desired result.

VBA Concatenate Strings Example2-5

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

Ampersand Operator Example2-7

Step 7: Now, let us execute the Code by pressing the F5 key or by clicking on the Play Button.

VBA Concatenate Strings Example2-6

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

VBA Concatenate Strings Example3-1

Step 2: Now declare two variables as String as shown below.

Code:

Sub Example3()

Dim Str1, Str2 As String

End Sub

VBA Concatenate Strings Example3-2

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

VBA Concatenate Strings Example3-3

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

Ampersand Operator Example3-4

Step 5: Run the code by pressing the F5 key or by clicking on the Play Button.

VBA Concatenate Strings Example3-5

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

VBA Concatenate Strings Example4-1

Step 2: Now declare three variables as Strings.

Code:

Sub Example4()

Dim Str1, Str2, Str3 As String

End Sub

VBA Concatenate Strings Example4-2

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

First Three Variables Example4-3

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

Addition Operator Example4-4

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

VBA Concatenate Strings Example4-5

Step 6: When we execute the code we can see the result.

VBA Concatenate Strings Example4-6

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 –

  1. VBA SendKeys
  2. VBA On Error Goto
  3. VBA Msgbox Yes/No
  4. 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

  1. First, enter the first string using double quotation marks.
  2. After that, type an ampersand.
  3. Next, enter the second text using double quotation marks.
  4. In the end, assign that value to a cell, or variable, or use a message box to see it.
string using double quotation
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.

concatenate using variables

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"))

totn Excel Functions


This Excel tutorial explains how to use the Excel & operator with syntax and examples.

Description

To concatenate multiple strings into a single string in Microsoft Excel, you can use the & operator to separate the string values.

The & operator can be used as a worksheet function (WS) and a VBA function (VBA) in Excel. As a worksheet function, the & operator can be entered as part of a formula in a cell of a worksheet. As a VBA function, you can use this operator in macro code that is entered through the Microsoft Visual Basic Editor.

Syntax

The syntax for the & operator is:

string1 & string2 [& string3 & string_n]

Parameters or Arguments

string1, string2, string3, … string_n
The string values to concatenate together.

Returns

The & operator returns a string/text value.

Applies To

  • Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000

Type of Function

  • Worksheet function (WS)
  • VBA function (VBA)

Example (as Worksheet Function)

Let’s look at some Excel & operator examples and explore hwo you would use the & operator as a worksheet function in Microsoft Excel:

Microsoft Excel

Based on the Excel spreadsheet above, the following & examples would return:

=A1 & A2
Result: "Alphabet"

="Tech on the " & "Net"
Result: "Tech on the Net"

=(A1 & "bet soup")
Result: "Alphabet soup"

Concatenate Space Characters

When you are concatenating values together, you might want to add space characters to separate your concatenated values. Otherwise, you might get a long string with the concatenated values running together. This makes it very difficult to read the results.

Let’s look at an easy example.

Microsoft Excel

Based on the Excel spreadsheet above, we can concatenate a space character using the & operator as follows:

=A1 & " " & A2
Result: "TechOnTheNet.com website"

In this example, we have used the & operator to add a space character between the values in cell A1 and cell A2. This will prevent our values from being squished together.

Instead our result would appear as follows:

"TechOnTheNet.com website"

Here, we have concatenated the values from the two cells (A1 and A2), separated by a space character.

Concatenate Quotation Marks

Since the & operator will concatenate string values that are enclosed in quotation marks, it isn’t straight forward how to add a quotation mark character to the concatenated results.

Let’s look at a fairly easy example that shows how to add a quotation mark to the resulting concatenated string using the & operator.

Microsoft Excel

Based on the Excel spreadsheet above, we can concatenate a quotation mark as follows:

="Apple " & """" & " Banana"
Result: Apple " Banana

In this example, we have used the & operator to add a quotation mark to the middle of the resulting string.

Since our strings to concatenate are enclosed in quotation marks, we use 2 additional quotation marks within the surrounding quotation marks to represent a quotation mark in our result as follows:

""""

Then when you put the whole function call together:

="Apple " & """" & " Banana"

You will get the following result:

Apple " Banana

Frequently Asked Questions

Question:For an IF statement in Excel, I want to combine text and a value.

For example, I want to put an equation for work hours and pay. If I am paid more than I should be, I want it to read how many hours I owe my boss. But if I work more than I am paid for, I want it to read what my boss owes me (hours*Pay per Hour).

I tried the following:

=IF(A2<0,"I owe boss" abs(A2) "Hours","Boss owes me" abs(A2)*15 "dollars")

Is it possible or do I have to do it in 2 separate cells? (one for text and one for the value)

Answer: There are two ways that you can concatenate text and values. The first is by using the & character to concatenate:

=IF(A2<0,"I owe boss " & ABS(A2) & " Hours","Boss owes me " & ABS(A2)*15 &  " dollars")

Or the second method is to use the CONCATENATE function:

=IF(A2<0,CONCATENATE("I owe boss ", ABS(A2)," Hours"), CONCATENATE("Boss owes me ", ABS(A2)*15,  " dollars"))

Example (as VBA Function)

Let’s look at some Excel & operator function examples and explore how to use the & operator in Excel VBA code:

The & operator can be used to concatenate strings in VBA code. For example:

Dim LValue As String

LValue = "Alpha" & "bet"

The variable LValue would now contain the value «Alphabet».

Frequently Asked Questions

Question:For an IF statement in Excel, I want to combine text and a value.

For example, I want to put an equation for work hours and pay. If I am paid more than I should be, I want it to read how many hours I owe my boss. But if I work more than I am paid for, I want it to read what my boss owes me (hours*Pay per Hour).

I tried the following:

=IF(A2<0,"I owe boss" abs(A2) "Hours","Boss owes me" abs(A2)*15 "dollars")

Is it possible or do I have to do it in 2 separate cells? (one for text and one for the value)

Answer: There are two ways that you can concatenate text and values. The first is by using the & character to concatenate:

=IF(A2<0,"I owe boss " & ABS(A2) & " Hours","Boss owes me " & ABS(A2)*15 &  " dollars")

Or the second method is to use the CONCATENATE function:

=IF(A2<0,CONCATENATE("I owe boss ", ABS(A2)," Hours"), CONCATENATE("Boss owes me ", ABS(A2)*15,  " dollars"))

Concatenation means joining two values or two strings together, similar to Excel. We use & or also known as an ampersand operator, to concatenate. For example, for two concatenate strings, we use & operators like String 1 and String 2. Now, there is an important thing to remember: while using the & operator, we need to provide spaces, or VBA will consider it long.

VBA Concatenate Strings

VBA concatenates one of those things we used to combine two or more value cell values. So if we say it in simple language, it is combined. It is joining two or more values together to have full value.

We have a function called CONCATENATE in excelThe CONCATENATE function in Excel helps the user concatenate or join two or more cell values which may be in the form of characters, strings or numbers.read more, which will combine two or more values or two or more cell values.

But in VBA, we do not have any built-in function to concatenate two or more values together. We do not even get to access the worksheet function class to access the VBA CONCATENATE function as a worksheet function.

Table of contents
  • VBA Concatenate Strings
    • How to Concatenate Strings in VBA?
    • Common Mistake in Ampersand VBA Concatenation
    • VBA Concatenate Using JOIN Function
    • Recommended Articles

VBA Concatenate

You are free to use this image on your website, templates, etc, Please provide us with an attribution linkArticle Link to be Hyperlinked
For eg:
Source: VBA Concatenate (wallstreetmojo.com)

How to Concatenate Strings in VBA?

Suppose we do not have any built-in function to concatenate values and even do not integrate the worksheet function with VBA. Now the challenge is, how do we combine values?

Even though there are no built-in functions, we can still combine them in VBA using the “ampersand” (&) symbol.

If you follow our posts regularly, we often use the ampersand (&) symbol in our coding.

For example, if you have the first and last names separately, we can combine these two and make them a full name. Then, follow the below steps to write the VBA macro code on our own.

Step 1: Go to Visual Basic Editor and create a VBA sub procedureSUB in VBA is a procedure which contains all the code which automatically gives the statement of end sub and the middle portion is used for coding. Sub statement can be both public and private and the name of the subprocedure is mandatory in VBA.read more.

Step 2: Define three variables as String.

Code:

Sub Concatenate_Example()

  Dim First_Name As String
  Dim Last_Name As String
  Dim Full_Name As String

End Sub

Step 3: Now, assign the variable’s first and last names.

Code:

Sub Concatenate_Example()

  Dim First_Name As String
  Dim Last_Name As String
  Dim Full_Name As String

  First_Name = "Sachin"
  Last_Name = "Tendulkar"

End Sub

Step 4: Now, combine these two names to the variable Full_Name using the ampersand variable.

Code:

Sub Concatenate_Example()

  Dim First_Name As String
  Dim Last_Name As String
  Dim Full_Name As String

  First_Name = "Sachin"
  Last_Name = "Tendulkar"

  Full_Name = First_Name & Last_Name

End Sub

Step 5: Now, show the value of the variable Full_Name in the message box.

Code:

Sub Concatenate_Example()

  Dim First_Name As String
  Dim Last_Name As String
  Dim Full_Name As String

  First_Name = "Sachin"
  Last_Name = "Tendulkar"

  Full_Name = First_Name & Last_Name

  MsgBox Full_Name

End Sub

Now, run the code. We will get the full name in the message box.

VBA Concatenate Example 1

The problem with this full name is we have not added a first name and last name separator character space. Therefore, combine space characters while combining the first and last names.

Code:

Sub Concatenate_Example()

  Dim First_Name As String
  Dim Last_Name As String
  Dim Full_Name As String

  First_Name = "Sachin"
  Last_Name = "Tendulkar"

  Full_Name = First_Name & " " & Last_Name

  MsgBox Full_Name

End Sub

It will give a proper full name now.

VBA Concatenate Example 1-1

Like this, using the ampersand symbol, we can concatenate values. Now, we will solve the worksheet problem of solving first name and last name together to make it a full name.

Example 2

Since we need to combine many names, we need to use loops to combine the first and last names. The below code will do the job for you.

Code:

Sub Concatenate_Example1()

  Dim i As Integer

  For i = 2 To 9
    Cells(i, 3).Value = Cells(i, 1) & " " & Cells(i, 2)
  Next i

End Sub

It will combine the first and last names, just like our VBA concatenate function.

Example 2-1

Common Mistake in Ampersand VBA Concatenation

If you notice my codes, we have added a space character between values and an ampersand symbol. It is essential because of the nature of VBA programming.

We cannot combine values and ampersand symbols. Otherwise, we will get a “Compile error” like the one below.

Example 2-2

VBA Concatenate Using JOIN Function

In VBA, we can use the JOIN function to combine values. First, look at the VBA JOINIn VBA, we use the Join command to join two or more strings together, similar to the concatenate function and the «&» command. read more function syntax.

VBA Join Formula

  • The array is nothing but an array that holds our values—for example, both first name & last name.
  • The delimiter is nothing but what is the separator between each array value, in this case, space character.

The below code will show the example of the same.

Code:

Sub Concatenate_Example2()

  Dim MyValues As Variant
  Dim Full_Name As String

  MyValues = Array("Sachin", "Tendulkar")
  Full_Name = Join(MyValues, " ")

  MsgBox Full_Name

End Sub

Recommended Articles

This article has been a guide to VBA Concatenate. Here, we learned how to use the Join function’s VBA concatenate function, some practical examples, and a downloadable Excel template. Below are some useful Excel articles related to VBA: –

  • VBA LEN
  • Concatenate Columns in Excel
  • Excel VBA Worksheet Function
  • Concatenate Date in Excel

Like this post? Please share to your friends:
  • Консолидация данных в excel с нескольких файлов
  • Коническая диаграмма в excel 2019
  • Консолидация данных в excel пример
  • Конечна строка в excel
  • Консолидация данных в excel по одной таблице