Excel Merge Data From Two Cells

So earlier we had CONCAT, and CONCATENATE function to concatenate multiple cells. But if we wanted to supply a range for joining cells with a delimiter (say a comma) then it is really tricky with these functions. But now Excel has introduced a new function called TEXTJOIN Function that can be used to concatenate ranges with a lot more flexibility.

  • You can merge (combine) rows from one table into another simply by pasting the data in the first empty cells below the target table. The table will increase in size to include the new rows. If the rows in both tables match up, you can merge the columns of one table with another—by pasting them in the first empty cells to the right of the table.
  • The fastest and easiest way to combine two or more cells in Excel is to use the built-in Merge and Center option. The whole process takes only 2 quick steps: Select the contiguous cells you want to combine. On the Home tab Alignment group, click the Merge & Center.
  • Move or combine multiple cell contents into one with TEXTJOIN function In Excel 2019 or Office 365, there is new TEXTJOIN function may help you to combine cell values from different cells into a single cell with ease. The generic syntax of the TEXTJOIN is: =TEXTJOIN (delimiter, ignoreempty, text1, text2, ).
  • VBA Code to Merge Cells. To merge cells with VBA, use a statement with the following.

In this article, we will learn how to concatenate cell values of a range with comma using TEXTJOIN function. For users who don't have this function we will discuss other methods of concatenating range values with comma.

Merge and Combine Columns without Losing Data in Excel. First of all, enable the Clipboard by clicking the Anchor button at the bottom-right corner of Clipboard group on the Home tab. See screenshot: 2. Select the columns you will merge, and press Ctrl + C keys to copy them. Note: You can also.

Generic Formula

=TEXTJOIN(',',TRUE,text_range1,[text_range2]...)

Comma (',') : This is the delimiter we want to use. Since in this article we are concentrating on concatenating cells with commas.

TRUE : For ignoring blank cells in the range.

Text_range1 : This is the range whose cells have values you want to concatenate.

[Text_range2] : The other ranges if you want to join in the text with commas.

Let’s see an example to make things clear.

Example: Join Cell Values of Ranges With Comma as Delimiter

Here, we have some values in range B2:E8. We need to join the texts of each cell in a row.

Let's implement the formula we have and drag it down.

=TEXTJOIN(',',TRUE,B3:E3)

You can see that we have a string which is a result of concatenation of texts with commas.

Let's say if you want to concatenate the range B3:E3 and B7:E7. So the formula will be:

=TEXTJOIN(',',TRUE,B3:E3,B7:E7)

It will concatenate all the texts ignoring the blank cells.

How does it work?

The formula is simple. The TEXTJOIN function requires the delimiter with which you want to join text with. The second variable is set to be true so that it ignores the blank cells.

Now if any cell has invisible values like space then you will see an extra comma in between the joined text.

To avoid spaces, use the TRIM function to strip them out.

=TEXTJOIN(',',TRUE,TRIM(B3:E3,B7:E7))

Concatenating Cells with Commas in Excel 2016 and Older

The problem is that the TEXTJOIN function is only available to Excel 2019 and 365. So if you want to concatenate the cells with commas, we'll need to use a trick.

Merge

So to concatenate cells in a row with commas do this.

In a cell, write '=' to start the formula and select the range as shown below.

Now press F2 and select the range in the formula bar or cell.

Press F9 key.

Now remove the equals and curly braces. You have the cells joined with commas.

But this way is not that effective for too many operations.

So do we have any other way to combine texts with a given delimiter in Excel? The other way is the VBA way. Let's create one UDF to do this.

Press CTRL+F11 to open the VB Editor. Right click on the workbook and insert a module. Copy the code above and paste in the module's code area.


Now use this formula to join text with any delimiter you want to.

This formula will work in any version of Excel. You can download the workbook below to use this formula immediately.

So yeah guys, this is how you can join text with comma delimiter in Excel. I hope it was helpful for you. If you have any questions regarding this topic or any other excel related topic, ask in the comments section below. Till then keep Excelling.

Related Articles:

Split Numbers and Text from String in Excel 2016 and Older: When we didn't have TEXTJOIN function we used LEFT and RIGHT functions with some other functions to split numeric and nonnumeric characters from a string.

Extract Text From A String In Excel Using Excel's LEFT And RIGHT Function: To remove text in excel from string we can use excel's LEFT and RIGHT function. These functions help us chop strings dynamically.

Remove leading and trailing spaces from text in Excel: Leading and trailing spaces are hard to recognize visually and can mess up your data. Stripping these characters from the string is a basic and most important task in data cleaning. Here's how you can do it easily in Excel.

Remove Characters From Right: To remove characters from the right of a string in Excel, we use the LEFT function. Yes, the LEFT function. The LEFT function retains the given number of characters from LEFT and removes everything from its right.

Remove unwanted characters in Excel: To remove unwanted characters from a string in Excel, we use the SUBSTITUTE function. The SUBSTITUTE function replaces the given characters with another given character and produces a new altered string.

How to Remove Text in Excel Starting From a Position in Excel: To remove text from a starting position in a string, we use the REPLACE function of Excel. This function help us determine the starting position and number of characters to strip.

Popular Articles:

50 Excel Shortcuts to Increase Your Productivity Get faster at your task. These 50 shortcuts will make you work even faster on Excel.

How to use Excel VLOOKUP Function This is one of the most used and popular functions of excel that is used to lookup value from different ranges and sheets.

How to use theExcel COUNTIF Function Count values with conditions using this amazing function. You don't need to filter your data to count specific values. Countif function is essential to prepare your dashboard.

How to Use SUMIF Function in Excel This is another dashboard essential function. This helps you sum up values on specific conditions.

In this VBA Tutorial, you learn how to merge cells and unmerge cells in a variety of ways.

This VBA Tutorial is accompanied by Excel workbooks containing the data and macros I use in the examples below. You can get immediate free access to these example workbooks by subscribing to the Power Spreadsheets Newsletter.

Use the following Table of Contents to navigate to the section you're interested in.

Related VBA and Macro Tutorials

The following VBA and Macro Tutorials may help you better understand and implement the contents below:

  • General VBA constructs and structures:

    • Learn about using variables here.

    • Learn about VBA data types here.

    • Learn about R1C1 and A1 style references here.

  • Practical VBA applications and macro examples:

    • Learn how to work with worksheets here.

You can find additional VBA and Macro Tutorials in the Archives.

#1: Merge Cells

VBA Code to Merge Cells

Cells

To merge cells with VBA, use a statement with the following structure:

Process Followed by VBA Code

VBA Statement Explanation

  1. Item: Worksheet.

    • VBA Construct: Workbook.Worksheets property.

    • Description: Returns a Worksheet object representing the worksheet you work with.

  2. Item: Range(“FirstCell:LastCell”).

    • VBA Construct: Worksheet.Range property.

    • Description: Returns a Range object representing the cell range between FirstCell and LastCell. This is the cell range you merge.

      Specify FirstCell and LastCell using an A1-style cell reference. Separate FirstCell and LastCell using the range operator, a colon (:). Enclose the entire cell range address within quotations (“”).

  3. Item: Merge.

    • VBA Construct: Range.Merge method.

    • Description: Merges the cells represented by the Range object you specify in item #2 above to create a merged cell.

Macro Example

The following macro merges cells A5 to E6 of the worksheet named “Merge Cells”.

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, cells A5 to E6 are merged.

#2: Unmerge Cells

VBA Code to Unmerge Cells

To unmerge cells with VBA, use a statement with the following structure:

How To Join Cells Together In Excel

Process Followed by VBA Code

VBA Statement Explanation

  • Item: Worksheet.

    • VBA Construct: Workbook.Worksheets property.

    • Description: Returns a Worksheet object representing the worksheet you work with.

  • Item: Range(“A1CellReference”).

    • VBA Construct: Worksheet.Range property.

    • Description: Returns a Range object representing a cell within the merged cell you unmerge. Specify the cell using an A1-style cell reference (A1CellReference) enclosed within quotations (“”).

  • Item: UnMerge.

    • VBA Construct: Range.UnMerge method.

    • Description: Separates the merged cell containing the cell you specify in item #2 above into individual regular cells.

Macro Example

The following macro unmerges the merged cell containing cell C6 of the worksheet named “Merge Cells”.

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, the merged cell containing cell C6 is unmerged into individual regular cells.

The merged cell range (A5 to E6) was originally merged using the macro example #1 above.

#3: Merge Cells and Center

VBA Code to Merge Cells and Center

To merge cells and center the contents with VBA, use a macro with the following statement structure:

Process Followed by VBA Code

VBA Statement Explanation

Lines #1 and #5: With Worksheet.Range(“FirstCell:LastCell”) End With

  1. Item: With… End With.

    • VBA Construct: With… End With statement.

    • Description: Statements within the With… End With statement (lines #2 through #4 below) are executed on the Range object returned by item #3 below.

  2. Item: Worksheet.

    • VBA Construct: Workbook.Worksheets property.

    • Description: Returns a Worksheet object representing the worksheet you work with.

  3. Item: Range(“FirstCell:LastCell”).

    • VBA Construct: Worksheet.Range property.

    • Description: Returns a Range object representing the cell range between FirstCell and LastCell. This is the cell range you merge.

      Specify FirstCell and LastCell using an A1-style cell reference. Separate FirstCell and LastCell using the range operator, a colon (:). Enclose the entire cell range address within quotations (“”).

Line #2: .HorizontalAlignment = xlCenter

Excel Merge Data From Two Cells
  1. Item: HorizontalAlignment = xlCenter.

    • VBA Construct: Range.HorizontalAlignment property.

    • Description: Horizontally centers the contents of the cell range you specify in line #1 above by setting the HorizontalAlignment property to xlCenter.

Line #3: VerticalAlignment = xlCenter

  1. Item: VerticalAlignment = xlCenter.

    • VBA Construct: Range.VerticalAlignment property.

    • Description: Vertically centers the contents of the cell range you specify in line #1 above by setting the VerticalAlignment property to xlCenter.

Line #4: Merge

  1. Item: Merge.

    • VBA Construct: Range.Merge method.

    • Description: Merges the cells represented by the Range object you specify in line #1 above to create a merged cell.

Macro Example

The following macro (i) centers the contents in cells A8 to E9 of the worksheet named “Merge Cells”, and (ii) merges those cells.

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA merges cells A8 to E9 and centers the contents.

#4: Merge Cells Across

VBA Code to Merge Cells Across

To merge cells across (in the same row) with VBA, use a statement with the following structure:

Process Followed by VBA Code

VBA Statement Explanation

  1. Item: Worksheet.

    • VBA Construct: Workbook.Worksheets property.

    • Description: Returns a Worksheet object representing the worksheet you work with.

  2. Item: Range(“FirstCell:LastCell”).

    • VBA Construct: Worksheet.Range property.

    • Description: Returns a Range object representing the cell range between FirstCell and LastCell. This is the cell range you merge.

      Specify FirstCell and LastCell using an A1-style cell reference. Separate FirstCell and LastCell using the range operator, a colon (:). Enclose the entire cell range address within quotations (“”).

  3. Item: Merge.

    • VBA Construct: Range.Merge method.

    • Description: Merges the cells in each row of the cell range you specify in item #2 above to create separate merged cells. For these purposes, considers the Across parameter (item #4 below).

  4. Item: Across:=True.

    • VBA Construct: Across parameter of the Range.Merge method.

    • Description: Specifies that the cells in each row of the cell range you specify in item #2 above are merged separately. In other words, the cells in each row are merged into separate merged cells (vs. a single merged cell covering the entire cell range).

      The default value of the Across parameter is False. In such case, all cells within the cell range you specify are merged into a single cell. This is the equivalent of simply merging cells (operation #1 above).

Macro Example

The following macro merges cells A11 to E15 of the worksheet named “Merge Cells” across. Therefore, the cells in each row from row 11 to row 15 are merged into separate merged cells.

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA merges cells A11 to E15 across.

#5: Merge Cells Based on Cell Value

VBA Code to Merge Cells Based on Cell Value

To merge cells based on a cell value (whether it meets certain criteria), use a macro with the following statement structure:

Process Followed by VBA Code

VBA Statement Explanation

Lines #1 and #5: With Worksheet End With

  1. Item: With… End With.

    • VBA Construct: With… End With statement.

    • Description: Statements within the With… End With statement (lines #2 through #4 below) are executed on the Worksheet object returned by item #2 below.

  2. Item: Worksheet.

    • VBA Construct: Workbook.Worksheets property.

    • Description: Returns a Worksheet object representing the worksheet you work with.

Lines #2 and #4: For Counter = LastRow To FirstRow Step -1 Next Counter

  1. Item: For… Next Counter.

    • VBA Construct: For… Next statement.

    • Description: Repeats the statements within the loop (line #3 below) for each row between FirstRow (item #4 below) and LastRow (item #3 below).

  2. Item: Counter.

    • VBA Construct: Counter of For… Next statement.

    • Description: Loop counter. If you explicitly declare a variable to represent the loop counter, use the Long data type.

  3. Item: LastRow.

    • VBA Construct: Counter Start of For… Next statement.

    • Description: Number of the last row (further down the worksheet) you want the macro to consider when identifying rows to merge cells. The number of the last row is also the initial value of Counter (item #2 above).

      If you explicitly declare a variable to represent the number of the last row to consider, use the Long data type.

  4. Item: FirstRow.

    • VBA Construct: Counter End of For… Next statement.

    • Description: Number of the first row (closer to the top of the worksheet) you want the macro to consider when identifying rows to merge cells. The number of the first row is also the final value of Counter (item (#2 above).

      If you explicitly declare a variable to represent the number of the first row to consider, use the Long data type.

  5. Item: Step -1.

    • VBA Construct: Step of For… Next statement.

    • Description: Amount by which Counter (item #2 above) changes every time a loop iteration occurs.

      In this scenario, you loop backwards: from LastRow (item #3 above) to FirstRow (item #4 above). Therefore, step is -1.

Line #3: If .Cells(Counter, CriteriaColumn).Value = Criteria Then .Range(.Cells(Counter, FirstColumn), .Cells(Counter, LastColumn)).Merge

  1. Item: If… Then.

    • VBA Construct: If… Then… Else statement.

    • Description: Conditionally executes the statement at the end of the line of code (items #5 through #8 below) if the condition specified in item #4 below is met.

  2. Item: .Cells(Counter, CriteriaColumn).

    • VBA Construct: Worksheet.Cells property and Range.Item property.

    • Description: Returns a Range object representing the cell at the intersection of row number Counter and column number CriteriaColumn.

      At any given time, the value of the loop counter (Counter) is the same as that of the row through which the macro is currently looping. CriteriaColumn is the number of the column containing the cells you consider for purposes of determining whether to merge cells in the row through which the macro is currently looping.

  3. Item: Value.

    • VBA Construct: Range.Value property.

    • Description: Returns the value of the cell represented by the Range object returned by item #2 above.

  4. Item: .Cells(Counter, CriteriaColumn).Value = Criteria.

    • VBA Construct: Condition of If… Then… Else statement.

    • Description: This condition is an expression that evaluates to True or False, as follows:

        • True: When the value of the cell represented by the Range object returned by item #2 above is equal to the criteria you specify (Criteria).

        • False: When the value of the cell represented by the Range object returned by item #2 above isn't equal to the criteria you specify (Criteria).

      If you explicitly declare a variable to represent value, ensure that the data type you use can handle the value you use as criteria.

  5. Item: .Range.

    • VBA Construct: Worksheet.Range property.

    • Description: Returns a Range object representing a cell range specified as follows:

      • Leftmost cell: Range object returned by item #6 below.

      • Rightmost cell: Range object returned by item #7 below.

  6. Item: .Cells(Counter, FirstColumn).

    • VBA Construct: Worksheet.Cells property and Range.Item property.

    • Description: Returns a Range object representing the cell at the intersection of row number Counter and column number FirstColumn.

      At any given time, the value of the loop counter (Counter) is the same as that of the row through which the macro is currently looping. FirstColumn is the number of the first column in the cell range you want the macro to merge. If you explicitly declare a variable to represent FirstColumn, use the Long data type.

  7. Item: .Cells(Counter, LastColumn).

    • VBA Construct: Worksheet.Cells property and Range.Item property.

    • Description: Returns a Range object representing the cell at the intersection of row number Counter and column number LastColumn.

      At any given time, the value of the loop counter (Counter) is the same as that of the row through which the macro is currently looping. LastColumn is the number of the last column in the cell range you want the macro to merge. If you explicitly declare a variable to represent LastColumn, use the Long data type.

  8. Item: Merge.

    • VBA Construct: Range.Merge method.

    • Description: Merges the cells represented by the Range object returned by items #5 through #7 above to create a merged cell.

Macro Example

The following macro merges cells in columns myFirstColumn through myLastColumn in each row between myFirstRow and myLastRow where the value stored in column myCriteriaColumn is the string held by the myCriteria variable.

  • myFirstRow is set to 5.

  • myLastRow is set to the number of the last row with data in the worksheet named “Merge Cells Based on Criteria”. The constructs used by the statement that finds the last row with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Row property.

  • Both myCriteriaColumn and myFirstColumn are set to 1 (column A).

  • myLastColumn is set to 5 (column E).

  • myCriteria holds the string “Merge cells”

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, VBA merges cells in columns A through E in each row where the value stored in column A is the string “Merge Cells”.

#6: Merge Cells Within a Row Based on Cell Value

VBA Code to Merge Cells Within a Row Based on Cell Value

To merge cells within a row based on a cell value (the cell value determines the number of cells to merge), use a macro with the following statement structure:

Process Followed by VBA Code

VBA Statement Explanation

Lines #1 and #5: With Worksheet End With

  1. Item: With… End With.

    • VBA Construct: With… End With statement.

    • Description: Statements within the With… End With statement (lines #2 through #4 below) are executed on the Worksheet object returned by item #2 below.

  2. Item: Worksheet.

    • VBA Construct: Workbook.Worksheets property.

    • Description: Returns a Worksheet object representing the worksheet you work with.

Excel Merge Data From Two Cells Together

Lines #2 and #4: For Counter = LastRow To FirstRow Step -1 Next Counter

Merge Two Columns Into One In Excel

  1. Item: For… Next Counter.

    • VBA Construct: For… Next statement.

    • Description: Repeats the statements within the loop (line #3 below) for each row between FirstRow (item #4 below) and LastRow (item #3 below).

  2. Item: Counter.

    • VBA Construct: Counter of For… Next statement.

    • Description: Loop counter. If you explicitly declare a variable to represent the loop counter, use the Long data type.

  3. Item: LastRow.

    • VBA Construct: Counter Start of For… Next statement.

    • Description: Number of the last row (further down the worksheet) you want the macro to consider when identifying rows to merge cells. The number of the last row is also the initial value of Counter (item #2 above).

      If you explicitly declare a variable to represent the number of the last row to consider, use the Long data type.

  4. Item: FirstRow.

    • VBA Construct: Counter End of For… Next statement.

    • Description: Number of the first row (closer to the top of the worksheet) you want the macro to consider when identifying rows to merge cells. The number of the first row is also the final value of Counter (item (#2 above).

      If you explicitly declare a variable to represent the number of the first row to consider, use the Long data type.

  5. Item: Step -1.

    • VBA Construct: Step of For… Next statement.

    • Description: Amount by which Counter (item #2 above) changes every time a loop iteration occurs.

      In this scenario, you loop backwards: from LastRow (item #3 above) to FirstRow (item #4 above). Therefore, step is -1.

Line #3: .Cells(Counter, BaseColumn).Resize(ColumnSize:=.Cells(Counter, SizeColumn).Value).Merge

  1. Item: .Cells(Counter, BaseColumn).

    • VBA Construct: Worksheet.Cells property and Range.Item property.

    • Description: Returns a Range object representing the cell at the intersection of row number Counter and column number BaseColumn.

      At any given time, the value of the loop counter (Counter) is the same as that of the row through which the macro is currently looping. BaseColumn is the number of the column you use as base for purposes of merging cells within the row through which the macro is currently looping.

  2. Item: Resize(ColumnSize:=.Cells(Counter, SizeColumn).Value).

    • VBA Construct: Range.Resize property.

    • Description: Returns a Range object representing a resized cell range. The Range object returned by Range.Resize is determined as follows:

      • Base Cell Range: The base Range object (prior to resizing) is that returned by item #1 above.

      • Row Size: The number of rows in the cell range returned by Range.Resize remain the same. In other words, the cell range where cells are merged is 1 row tall.

        This is because the first parameter of Resize (known as RowSize) is omitted. Therefore, the number of rows in the cell range remains the same.

      • Column Size: The number of columns in the cell range returned by Range.Resize is determined by item #3 below.

  3. Item: ColumnSize:=.Cells(Counter, SizeColumn).Value.

    • VBA Constructs: ColumnSize parameter of Range.Resize property and Range.Value property.

    • Description: Specifies the number of columns in the Range object returned by the Range.Resize property. The number of columns in this cell range is equal to the value within the cell at the intersection of row number Counter and column number SizeColumn (.Cells(Counter, SizeColumn).Value).

      At any given time, the value of the loop counter (Counter) is the same as that of the row through which the macro is currently looping. SizeColumn is the number of the column containing the number of cells you want to merge within the row through which the macro is currently looping.

  4. Item: Merge.

    • VBA Construct: Range.Merge method.

    • Description: Merges the cells represented by the Range object returned by items #1 through #3 above.

Macro Example

The following macro merges a certain number of cells, starting with the cell in column myBaseColumn, in each row between myFirstRow and myLastRow. The number of merged cells is equal to the value stored in mySizeColumn. If that value is 1, no cells are merged.

In other words, the macro merges the cells between column number mySizeColumn and the column whose number is equal to that stored within the cell in myBaseColumn.

Combine Cell Data Excel

  • myFirstRow is set to 5.

  • myLastRow is set to the number of the last row with data in the worksheet named “Merge Cells Based on Cell Value”. The constructs used by the statement that finds the last row with data in the worksheet are the Worksheet.Cells property, the Range.Find method, and the Range.Row property.

  • Both myBaseColumn and mySizeColumn are set to 1 (column A).

Effects of Executing Macro Example

The following GIF illustrates the results of executing this macro example. As expected, for each row with data, the macro merges the cells between column A and the column whose number is specified in column A.

References to VBA Constructs Used in this VBA Tutorial

Merge Cells And Keep Text

Use the following links to visit the appropriate webpage within the Microsoft Office Dev Center:

  1. Identify the worksheet you work with:

    • Workbook.Worksheets property.

  2. Return a Range object representing the cells you merge:

    • Worksheet.Range property.

    • Worksheet.Cells property.

    • Range.Resize property.

  3. Merge cells:

    • Range.Merge method.

  4. Unmerge cells:

    • Range.UnMerge method.

  5. Center the contents of a cell range horizontally or vertically:

    • Range.HorizontalAlignment property.

    • Range.VerticalAlignment property.

  6. Identify last row with data in a worksheet:

    • Range.Find method.

    • Range.Row property.

  7. Loop through rows:

    • For… Next statement.

  8. Identify the value stored in a cell to specify criteria for merging cells.

    • Range.Value property.

  9. Test if cells meet criteria for merging:

    • If… Then… Else statement.

  10. Work with variables:

    • Dim statement.

    • Set statement.

    • Data types:

      • Long data type.

      • String data type.

  11. Simplify object references:

    • With… End With statement.