How to quickly merge adjacent rows with same data in Excel?

Supposing you have a worksheet with same data in the adjacent rows, and now you want to merge the same cells into one cell, so that the data looks neat and beautiful. (See following screenshots.) How do you merge adjacent rows with same data quickly and conveniently? Today, I will introduce you some quick way to solve this problem.

Merge adjacent rows of same data with VBA code

Merge adjacent rows of same data with Kutools for Excel


arrow blue right bubble Merge adjacent rows of same data with VBA code


Of course you can merge the same data with Merge & Center command, but if there are hundreds of cells need to be merged, this method will be time-consuming. So the following VBA code can help you merge the same data easily.

1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following macro in the Modulewindow.

Sub MergeSameCell()
'Updateby20131127
Dim Rng As Range, xCell As Range
Dim xRows As Integer
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
xRows = WorkRng.Rows.Count
For Each Rng In WorkRng.Columns
    For i = 1 To xRows - 1
        For j = i + 1 To xRows
            If Rng.Cells(i, 1).Value <> Rng.Cells(j, 1).Value Then
                Exit For
            End If
        Next
        WorkRng.Parent.Range(Rng.Cells(i, 1), Rng.Cells(j - 1, 1)).Merge
        i = j - 1
    Next
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

3. Then press the F5 key to run this code, a dialog is displayed on the screen for selecting a range to work with. See screenshot:

doc-merge-same-data7

4. Then click OK, the same data in column A will be merged and centered. See screenshots:


arrow blue right bubble Merge adjacent rows of same data with Kutools for Excel

With the Merge same cells utility of Kutools for Excel, you can quickly merge the same values in multiple columns with one click.

Kutools for Excel includes more than 120 handy Excel tools. Free to try with no limitation in 30 days. Get it Now.

After installing Kutools for Excel, you can do as follows:

1. Select the columns that you want to merge the adjacent rows with same data.

2. Click Kutools > Range Converter > Merge Same Cells, see screenshot:

And the same data in the selected columns have been merged in one cell.

To know more about this, please visit this Merge Same Cells feature.


Related article:

Unmerge cells and fill with duplicate values in Excel

 

Recommended Productivity Tools

Office Tab

gold star1 Bring handy tabs to Excel and other Office software, just like Chrome, Firefox and new Internet Explorer.

Kutools for Excel

gold star1 Amazing! Increase your productivity in 5 minutes. Don't need any special skills, save two hours every day!

gold star1 200 New Features for Excel, Make Excel Much Easy and Powerful:

  • Merge Cell/Rows/Columns without Losing Data.
  • Combine and Consolidate Multiple Sheets and Workbooks.
  • Compare Ranges, Copy Multiple Ranges, Convert Text to Date, Unit and Currency Conversion.
  • Count by Colors, Paging Subtotals, Advanced Sort and Super Filter,
  • More Select/Insert/Delete/Text/Format/Link/Comment/Workbooks/Worksheets Tools...

Screen shot of Kutools for Excel

btn read more btn download btn purchase

Comments  

Permalink +2 Violeta
How do I replicate the VBA macro to merge adjacent cells in columns instead of rows?

Thanks
2014-08-15 09:32 Reply Reply with quote Quote
Permalink -3 rafael
Violeta, I duplicate the row (below).

eg eg try try
eg eg try try

And chage the code to this:


Next
WorkRng.Parent.Range(Rng.Cells(1, i), Rng.Cells(1, j - 1)).Merge
i = j - 1


It merged the row above to "eg" and "try"
2014-10-24 12:45 Reply Reply with quote Quote
Permalink 0 James
for anyone still trying to achieve this, I think I've got it

Start of Code
*************************************

Sub MergeSameCell()
'Updateby20131127
Dim Rng As Range, xCell As Range
Dim xRows As Integer
xTitleId = "MergeSimilar"
Set WorkRng = Application.Selection
Set WorkRng = Application.Inp utBox("Range", xTitleId, WorkRng.Address , Type:=8)
Application.ScreenUpdating = False
Application.DisplayAlerts = False

'xRows = WorkRng.Rows.Count
xCols = WorkRng.Columns.Count

'For Each Rng In WorkRng.Columns
' For i = 1 To xRows - 1
' For j = i + 1 To xRows
' If Rng.Cells(i, 1).Value Rng.Cells(j, 1).Value Then
' Exit For
' End If
' Next
' WorkRng.Parent. Range(Rng.Cells (i, 1), Rng.Cells(j - 1, 1)).Merge
' i = j - 1
' Next
'Next

For Each Rng In WorkRng.Rows
For i = 1 To xCols - 1
For j = i + 1 To xCols
If Rng.Cells(1, i).Value Rng.Cells(1, j).Value Then
Exit For
End If
Next
WorkRng.Parent. Range(Rng.Cells (1, i), Rng.Cells(1, j - 1)).Merge
i = j - 1
Next
Next

Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

*************************************
End of Code


I.E. Simply modify the code to swap any row references for column references
2017-02-17 17:21 Reply Reply with quote Quote
Permalink -1 Siva
Thanks a lot!!! helped me in a crucial time
2014-11-05 20:23 Reply Reply with quote Quote
Permalink -1 Hector
This has been useful to me so many times :)
Thanks a lot, it saved me a lot of time of work.

I have a small request. I'm trying to find the way to do the same merge, but when there are empty cells below each value, to merge each cell with all empty cells below.

How can I modify the Macro?

Thank you in advance
2014-11-26 09:39 Reply Reply with quote Quote
Permalink 0 A.Afifi
Try this Code




Sub MergeSameCell()

Dim Rng As Range, xCell As Range
Dim xRows As Integer

xTitleId = "KutoolsforExce l"

Set WorkRng = Application.Sel ection
Set WorkRng = Application.Inp utBox("Range", xTitleId, WorkRng.Address , Type:=8)

Application.Scr eenUpdating = False
Application.Dis playAlerts = False

xRows = WorkRng.Rows.Co unt
For Each Rng In WorkRng.Columns
For i = 1 To xRows - 1
For j = i + 1 To xRows
If Rng.Cells(i, 1).Value Rng.Cells(j, 1).Value Then
Exit For
End If
Next

If Not IsEmpty(Rng.Cel ls(i, 1).Value) Or Not IsEmpty(Rng.Cel ls(j - 1, 1).Value) Then
WorkRng.Parent. Range(Rng.Cells (i, 1), Rng.Cells(j - 1, 1)).Merge
End If
i = j - 1
Next
Next

Application.Dis playAlerts = True
Application.Scr eenUpdating = True
End Sub
2016-10-05 04:48 Reply Reply with quote Quote
Permalink 0 A.Afifi
try this code

Sub MergeSameCell()

Dim Rng As Range, xCell As Range
Dim xRows As Integer

xTitleId = "KutoolsforExce l"

Set WorkRng = Application.Sel ection
Set WorkRng = Application.Inp utBox("Range", xTitleId, WorkRng.Address , Type:=8)

Application.Scr eenUpdating = False
Application.Dis playAlerts = False

xRows = WorkRng.Rows.Co unt
For Each Rng In WorkRng.Columns
For i = 1 To xRows - 1
For j = i + 1 To xRows
If Rng.Cells(i, 1).Value Rng.Cells(j, 1).Value Then
Exit For
End If
Next

If Not IsEmpty(Rng.Cel ls(i, 1).Value) Or Not IsEmpty(Rng.Cel ls(j - 1, 1).Value) Then
WorkRng.Parent. Range(Rng.Cells (i, 1), Rng.Cells(j - 1, 1)).Merge
End If
i = j - 1
Next
Next

Application.Dis playAlerts = True
Application.Scr eenUpdating = True
End Sub
2016-10-05 04:50 Reply Reply with quote Quote
Permalink -1 cp
If i have same name lie
Raju 1000
Raju 2000
Monu 100
Monu 200
Then how can i do marge name with amount
2015-02-17 05:58 Reply Reply with quote Quote
Permalink 0 Michal
Hi when running this macro i get the
"Application-de fined or object-defined error"

at the line

WorkRng.Parent. Range(rng.Cells (i, 1), rng.Cells(j - 1, 1)).Merge

Any ideas how to fix that?

Sincerely,
Michal
2015-05-15 10:52 Reply Reply with quote Quote
Permalink 0 Neo
I get same error.


have you figure that out yet? if you do, how did u do?

thanks
2015-06-17 17:32 Reply Reply with quote Quote
Permalink 0 Hector Oses
I leave here the script modified so it will merge cells below with the same value or with empty cell:

Sub MergeSameCell()
'Updateby20131127
Dim Rng As Range, xCell As Range
Dim xRows As Integer

xTitleId = "KutoolsforExce l"
Set WorkRng = Application.Sel ection
Set WorkRng = Application.Inp utBox("Range", xTitleId, WorkRng.Address , Type:=8)

Application.Scr eenUpdating = False
Application.Dis playAlerts = False

xRows = WorkRng.Rows.Co unt
For Each Rng In WorkRng.Columns
For i = 1 To xRows - 1
For j = i + 1 To xRows
If Rng.Cells(j, 1).Value "" Then
If Rng.Cells(i, 1).Value Rng.Cells(j, 1).Value Then
Exit For
End If
End If
Next

WorkRng.Parent. Range(Rng.Cells (i, 1), Rng.Cells(j - 1, 1)).Merge
i = j - 1
Next
Next

Application.Dis playAlerts = True
Application.Scr eenUpdating = True

End Sub
2015-08-03 11:47 Reply Reply with quote Quote
Permalink 0 Jimmy
Hi please help. I have problem with the code, with this line. Anything wrong?

If Rng.Cells(j, 1).Value "" Then
2016-12-04 07:40 Reply Reply with quote Quote
Permalink 0 SonGokussj4
Hi there. You should have:

if Rng.Cells(j, 1).Value = "" then...
2017-03-06 16:27 Reply Reply with quote Quote
Permalink 0 hasan
Merging nice. But it requires to specifying the range while running the code. I want to specify the range i.e. B1:B50 in the vba code. And making it top left alignment but how please help.
2015-10-20 17:26 Reply Reply with quote Quote
Permalink 0 hasan
While runing the above code then shows compile error: syntax error. In the line where "" used and lower line of this.
2015-10-20 17:32 Reply Reply with quote Quote
Permalink 0 Jimmy
I too encounter this issue with this line.

If Rng.Cells(j, 1).Value "" Then

Can someone help?
2016-12-04 07:42 Reply Reply with quote Quote
Permalink 0 Debdatta DEY
hello,
How can I put range automatically without user input
2016-04-28 15:30 Reply Reply with quote Quote
Permalink 0 Hector Oses
I'm sorry but I got somebody else to do the scripting for me, I have no knowledge to help you with the modifications.
2016-05-10 22:04 Reply Reply with quote Quote
Permalink 0 PURUSOTHAMAN
Hi sir,

. I try the vba code but it not working. Error message for.408.
Particularly that the comment
WorkRng.Parent. Range(rng.Cells (i, 1), rng.Cells(j - 1, 1)).Merge.
Please send the solution. I spend lot of time merge the documents.
I am mostly merged this format of cells
C20059290.

Thanks and regards
Purusothaman. C
2016-08-05 19:08 Reply Reply with quote Quote
Permalink 0 PURUSOTHAMAN
Dear sir,

. I am using vba code for excel sheet for merge cells. It not working came for 408 error. Particularly this code
WorkRng.Parent. Range(rng.Cells (i, 1), rng.Cells(j - 1, 1)).Merge.
Give the solution.
Thanks and regards
Purusothaman
2016-08-05 19:15 Reply Reply with quote Quote
Permalink 0 Tharaka
Hi,

Can some one instruct with reverse engineering - demarging cells with populating same value for all.
2016-08-31 05:55 Reply Reply with quote Quote
Permalink 0 Xandre
Hi,

The makro works, but now when I want to filter on the column A, only the first Information from column B applicable to column A are seen.

Looking at the example given in the makro, if I want to filter on Monday after the merge was done, only Nicol will Display and no info from Lucy and Lily are displayed.

Is there a line I can add to avoid this?
2016-10-26 07:21 Reply Reply with quote Quote
Permalink 0 SUMAN PAUL
In EXCEL

INPUT

NAME PRO1 PRO2 PRO3
A
B
C



output

A PRO1
A PRO2
A PRO3
B PRO1
B PRO2
B PRO3
C PRO1
C PRO2
C PRO3
2017-05-17 12:00 Reply Reply with quote Quote

Add comment


Security code
Refresh