Excel VBA Concatenation Operators
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 :
Attention reader! Don’t stop learning now. If you are an Excel beginner (or an intermediate) and want to learn Excel, Geeksforgeeks brings the perfect course for you to start, Diving Into Excel
In this article, we are going to see about concatenate operators and how to use VBA to concatenate strings as well as numbers.
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 :
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.
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
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
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