Excel VBA to Concatenate Cell Values 

Get FREE Advanced Excel Exercises with Solutions!

While working on Excel, sometimes we need to concatenate cell values. We can use Excel VBA to concatenate cell values easily. For example, if we have a dataset containing Salesperson ID and Salesperson, we can concatenate those values using different methods in Excel VBA. In this article, I’m going to demonstrate to you how we can use Excel VBA to Concatenate Cell Values. To get an overview of how it works, you can watch the following video.


Download Practice Workbook

You can download our practice workbook from here for free!


How to Launch VBA Editor in Excel

As we’ll use the VBA environment throughout this work, we need to enable the VBA setup first if not done so.

Using Module Window:

First of all, open the Developer tab. If you don’t have the developer tab, then you have to enable the developer tab. Then select the Visual Basic command.

Developer Tab Opening for excel vba target cell 

Hence, the Visual Basic window will open. Then, we’ll insert a Module from the Insert option where we’ll write the VBA Code.

Inserting Module 


1. Using Ampersand (&) to Concatenate Cell Values

We have a dataset that contains Salesperson ID and Salesperson. Now we need to concatenate the values in the cells B5 and C5. The following image shows the dataset.

Dataset for Using Ampersand (&) to Concatenate Cell Values 

We can use the Ampersand sign (&) in the VBA code to concatenate the cell values. I’ve shown the VBA code in the following image.

Code for Using Ampersand (&) to Concatenate Cell Values 

Sub concatenateWithAmpersand()
    Dim salespersonID As String
    Dim salesperson As String
    Dim concatenatedWithAmp As String
    salespersonID = Range("B5").Value
    salesperson = Range("C5").Value
    concatenatedWithAmp = salespersonID & " " & salesperson
    Range("D5").Value = concatenatedWithAmp
End Sub

Code Breakdown:

Sub concatenateWithAmpersand()

This statement creates a sub-procedure named concatenateWithAmpersand.

Dim salespersonID As String
Dim salesperson As String
Dim concatenatedWithAmp As String

Three variables are declared to hold the values of the cell B5, C5, and their concatenated values respectively.

salespersonID = Range("B5").Value
salesperson = Range("C5").Value

These statements assign the cell values to the variables.

concatenatedWithAmp = salespersonID & " " & salesperson

The variable values are now concatenated using the Ampersand sign (&) and assigned to the concatenatedWithAmp variable.

Range("D5").Value = concatenatedWithAmp

The value of the concatenatedWithAmp variable is stored in cell D5.

End Sub

The sub-procedure ends.

Now if we run the macro, the values in the cells B5 and C5 will be concatenated and stored in the cell D5. The following video contains the demonstration.

You can repeat this step by making necessary adjustments in the code to concatenate the other cell values as well.

If you have thousands of cells will you concatenate each cell manually? Definitely not! VBA offers you loops to iterate through cells and perform the task.

We can use a single macro to automatically concatenate column-wise cell values.

Our dataset contains the Salesperson ID and the Salesperson as the header. The following image shows our dataset.

Dataset for Automatically Concatenating Column Wise Series of Cell Values in Excel VBA 

Now we want to concatenate every Salesperson ID with their corresponding Salesperson. We’ll use VBA to do so.

The following image is the image of the macro that we’ll use to perform the task.

Code for Automatically Concatenating Column Wise Series of Cell Values in Excel VBA 

Sub ConcatenateColumnsAutomatically()
    Dim lastRow As Long
    Dim startingRow As Long
    lastRow = Cells(Rows.Count, "B").End(xlUp).Row
    For startingRow = 5 To lastRow
        Range("D" & startingRow).Value = Range("B" & startingRow).Value & " " & Range("C" & startingRow).Value
    Next startingRow
End Sub

Code Breakdown:

Sub ConcatenateColumnsAutomatically()

The sub-procedure begins.

Dim lastRow As Long
Dim startingRow As Long

            I’ve declared two variables to hold the values of the last and first used cells in column B.

lastRow = Cells(Rows.Count, "B").End(xlUp).Row

This statement computes the value of the last used cell in column B.

For startingRow = 5 To lastRow
Range("D" & startingRow).Value = Range("B" & startingRow).Value & " " & Range("C" & startingRow).Value
Next startingRow

This block of statements concatenates the cells in columns B and C in the same row and stores the concatenated value in the corresponding cell of column D. And it repeats the process for the row range starting from 5 to the lastRow.

End Sub

The sub-procedure ends.

If we run the macro now, all the cells in columns B and C will concatenate starting from row 5 to the lastRow. And the concatenated values will be stored in the corresponding cells in column D.


2. Concatenating Cell Values Using Plus (+) Operator

We can also use the Plus operator (+) to concatenate cell values. We’ve got the same dataset as we can see from the following image.

Dataset for Concatenating Cell Values Using Plus (+) Operator 

Now, we’ll use the Plus operator (+) instead of the Ampersand sign (&) to concatenate cell values.

The VBA code to do so is as follows.

Code for Concatenating Cell Values Using Plus (+) Operator 

Sub ConcatenateColumnsWithPlusSign()
    Dim lastRow As Long
    Dim startingRow As Long
    lastRow = Cells(Rows.Count, "B").End(xlUp).Row
    For startingRow = 5 To lastRow
        Range("D" & startingRow).Value = Range("B" & startingRow).Value + " " + Range("C" & startingRow).Value
    Next startingRow
End Sub

Code Breakdown:

Sub ConcatenateColumnsWithPlusSign()

The sub-procedure named ConcatenateColumnsWithPlusSign() starts with this statement.

Dim salespersonID As String
Dim salesperson As String
Dim concatenatedWithPlus As String

I’ve declared three variables to hold the values of the cells B5, C5, and their concatenated value respectively.

salespersonID = Range("B5").Value
salesperson = Range("C5").Value

These statements assign the values of the cells B5 and C5 to the respective variables.

concatenatedWithPlus = salespersonID + " " + salesperson

The values of the cells are concatenated and stored in the variable named concatenatedWithPlus.

Range("D5").Value = concatenatedWithPlus

The value of the concatenatedWithPlus variable is stored in cell D5.

End Sub

The sub-procedure ends.

Now, if we run this macro, we can see that the values in cells B5 and C5 are concatenated and stored in cell D5. You can check the following video to observe this.


3. Automatically Concatenating Row Wise Range of Cell Values in Excel VBA

We can concatenate the cell values row-wise too. In this section, I’m going to demonstrate to you two such examples.


3.1 Populating Concatenated Cell Values in A Single Cell

We can concatenate the cell values row-wise and store them in a single cell. The following image represents our dataset.

Dataset for Populating Concatenated Cell Values in A Single Cell 

In this dataset, we’ve Salesperson ID, Salesperson, Product, Product ID, and Total Sales data. Now we want to get a Salesperson ID from the user and concatenate all the data associated with the Salesperson ID and finally store them in a single cell.

We’ll use VBA to do so. The following image contains the code to do so.

Code for Populating Concatenated Cell Values in A Single Cell 

Sub ConcatenateSalespersonData()
    Dim salespersonID As Variant
    Dim dataRange As Range
    Dim row As Range
    Dim result As String
    salespersonID = InputBox("Please enter the Salesperson ID:")
    Set dataRange = Range("B:F").Find(What:=salespersonID, LookIn:=xlValues, LookAt:=xlWhole)
    If dataRange Is Nothing Then
        MsgBox "Salesperson ID " & salespersonID & " was not found."
        Exit Sub
    End If
    For Each row In Range(dataRange, Range("F" & Rows.Count).End(xlUp)).Rows
        If row.Cells(1, 1).Value = salespersonID Then
            result = result & " "
                For col = 2 To 6
                    result = result & row.Cells(1, col).Value & " "
                Next col
        End If
    Next row
    Range("G5").Value = salespersonID & " " & Trim(result)
    Range("G5").EntireColumn.AutoFit
End Sub

Code Breakdown:

Sub ConcatenateRowAutomatically()

This statement creates a sub-procedure named ConcatenateRowAutomatically.

Dim rng As Range
Dim i As String
Dim SourceRange As Range

I’ve declared three variables. Here rng will be used to loop through each cell in the SourceRange, i will be used to store the concatenated values and SourceRange will store the range of cells that we want to concatenate.

Set SourceRange = Range("B5:F5")

This statement sets the value of SourceRange to the range of cells B5 to F5. This is the range of cells that we want to concatenate.

For Each rng In SourceRange
i = i & rng & " "
Next rng

This loop goes through each cell in the SourceRange and concatenates the value of the cell with the variable i.

Range("G5").Value = Trim(i)

This statement removes any leading or trailing spaces from the concatenated string before storing the value in cell G5.

End Sub

The sub-procedure ends.

If we run the macro now, all the cell values in the range B5:F5 that I’ve specified in the VBA code will be concatenated. And the concatenated value will be stored in cell G5.

The following video demonstrates the process.


3.2 Displaying Concatenated Cell Values in Separate Lines

In this example, we’ll show the concatenated cell values in separate lines in a message box.

The dataset looks like this.

Dataset for Displaying Concatenated Cell Values in Separate Lines 

We’ll concatenate the cell values ranging from B5:F5 and display them in a message box. We’ll use VBA to do so.

Our code looks like the following image.

Code for Displaying Concatenated Cell Values in Separate Lines 

Sub concatenateAndDis()
    Dim salespersonID As String
    Dim salesperson As String
    Dim product As String
    Dim productID As String
    Dim totalSales As Long
    salespersonID = Range("B5").Value
    salesperson = Range("C5").Value
    product = Range("D5").Value
    productID = Range("E5").Value
    totalSales = Range("F5").Value
    MsgBox "Salesperson ID = " & salespersonID & vbCrLf & "Salesperson = " & salesperson & 
    vbCrLf & "Product = " & product & vbCrLf & "Product ID = " & productID & vbCrLf & "Total
    Sales = " & totalSales
End Sub

Code Breakdown:

Sub concatenateAndDis()

This statement creates a sub-procedure named concatenateAndDis.

Dim salespersonID As String
Dim salesperson As String
Dim product As String
Dim productID As String
Dim totalSales As Long

I’ve declared these five variables to store the cell values of the range B5:F5 respectively.

salespersonID = Range("B5").Value
salesperson = Range("C5").Value
product = Range("D5").Value
productID = Range("E5").Value
totalSales = Range("F5").Value

I’ve assigned the cell values to the corresponding variables.

MsgBox "Salesperson ID = " & salespersonID & vbCrLf & "Salesperson = " & salesperson & vbCrLf & "Product = " & product & vbCrLf & "Product ID = " & productID & vbCrLf & "Total Sales = " & totalSales

This statement concatenates the cell values and displays the concatenated values in separate lines in a message box.

End Sub

The sub-procedure ends.

Now if we run this macro, all the cell values in the range B5:F5 will be concatenated and display the concatenated value in different lines in the message box.

The following video demonstrates the process.


4. Concatenating Column Wise Unique Cell Values

Let’s think about a dataset with repeating values. The following image represents such a dataset.

Dataset for Concatenating Column Wise Unique Cell Values 

In this dataset, the Product header has repeating values. Now we want to concatenate the cell values but all unique values just for once.

The following image contains the VBA code to do so.

Code for Concatenating Column Wise Unique Cell Values 

Sub ConcatUniqueProducts()
    Dim lastRow As Long
    Dim productNames As Object
    Dim cell As Range
    Dim uniqueProductNames As String
    lastRow = Cells(Rows.Count, "D").End(xlUp).row
    Set productNames = CreateObject("Scripting.Dictionary")
    For Each cell In Range("D5:D" & lastRow)
        If Not productNames.exists(cell.Value) Then
            productNames.Add cell.Value, ""
        End If
    Next cell
    For Each Key In productNames.keys
        uniqueProductNames = uniqueProductNames & Key & ", "
    Next Key
    uniqueProductNames = Left(uniqueProductNames, Len(uniqueProductNames) - 2)
    MsgBox "The unique product names are:" & vbCrLf & uniqueProductNames
End Sub

Code Breakdown:

Sub ConcatUniqueProducts()

This statement creates a sub-procedure named ConcatUniqueProducts.

Dim lastRow As Long
Dim productNames As Object
Dim cell As Range
Dim uniqueProductNames As String

Here, lastRow will be used to store the last row number in column D.

The variable productNames will be used to store unique product names.

The variable cell will be used to loop through the cells in column D.

And the variable uniqueProductNames will be used to store the unique product names as a concatenated string.

lastRow = Cells(Rows.Count, "D").End(xlUp).Row

This statement finds out the last used cell in column D and stores the value in the lastRow variable.

Set productNames = CreateObject("Scripting.Dictionary")

This statement creates a dictionary object to store the unique product names.

For Each cell In Range("D5:D" & lastRow)
If Not productNames.exists(cell.Value) Then
productNames.Add cell.Value, ""
End If
Next cell

This block of statements loops through column D starting from row no 5 to the lastRow. And it checks whether the product name exists in the dictionary object. If not, then it adds the product name as a key in the dictionary object.

For Each Key In productNames.keys
uniqueProductNames = uniqueProductNames & Key & vbCrLf
Next Key

This block of statements loops through the keys in the dictionary object and concatenates the unique product names with a line break.

MsgBox "The unique product names are:" & vbCrLf & uniqueProductNames

This statement displays a message box with the unique product names in the dataset.

End Sub

The sub-procedure ends.

Now if we run the macro, the unique values from column D will be concatenated and displayed in a message box. 

The following video is a demonstration of this. 


Takeaways from This Article

If you’ve followed this article thoroughly, you’ll be now able to:

  • Concatenate row-wise cell values.
  • Concatenate column-wise cell values.
  • Concatenate unique values.

Conclusion

In this article, I’ve demonstrated how we can use Excel VBA to concatenate cell values. I’ve covered examples of concatenating row-wise cell values, column-wise cell values, and unique values. I’ve explained the VBA code as simply as possible so that you can understand them easily. I hope, with a little bit of practice, you can now use this concept of concatenating cell values yourself. If you face any issues regarding this, please let me know in the comment section. The Exceldemy team will be happy to solve those issues. Have a good day!

Hadi Ul Bashar
Hadi Ul Bashar

I'm Hadi Ul Bashar, an Engineering aspirant. I always look forward to keeping myself up to date in my area of interest. Currently, I'm happy to work at Exceldemy as an Excel & VBA Content Developer. My goal here is to provide an easy and detailed solution to whatever problem you may face in Excel. I hope I will make your Excel life easy as I'm always open to solve new problem!

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo