Note: The other languages of the website are Google-translated. Back to English

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. 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

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()
'Updateby Extendoffice
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 cells 2

4. Then click OK, the same data in column A will be merged together. See screenshot:

doc merge same cells 1


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 : with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. 

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 > Merge & Split > Merge Same Cells, see screenshot:

3. And then the same data in the selected columns have been merged in one cell. See screenshot:

doc merge same cells 4

Click to Download Kutools for Excel and free trial Now!

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


Demo:Merge same cells into one cell or unmerge to fill duplicate values:

Kutools for Excel: with more than 300 handy Excel add-ins, free to try with no limitation in 30 days. Download and free trial Now!

The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Comments (40)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
How do I replicate the VBA macro to merge adjacent cells in columns instead of rows? Thanks
This comment was minimized by the moderator on the site
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"
This comment was minimized by the moderator on the site
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.InputBox("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
This comment was minimized by the moderator on the site
Thanks a lot!!! helped me in a crucial time
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Try this Code Sub MergeSameCell() 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 If Not IsEmpty(Rng.Cells(i, 1).Value) Or Not IsEmpty(Rng.Cells(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.DisplayAlerts = True Application.ScreenUpdating = True End Sub
This comment was minimized by the moderator on the site
try this code Sub MergeSameCell() 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 If Not IsEmpty(Rng.Cells(i, 1).Value) Or Not IsEmpty(Rng.Cells(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.DisplayAlerts = True Application.ScreenUpdating = True End Sub
This comment was minimized by the moderator on the site
If i have same name lie Raju 1000 Raju 2000 Monu 100 Monu 200 Then how can i do marge name with amount
This comment was minimized by the moderator on the site
Hi when running this macro i get the "Application-defined 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
This comment was minimized by the moderator on the site
I get same error. have you figure that out yet? if you do, how did u do? thanks
This comment was minimized by the moderator on the site
I am getting the same error
This comment was minimized by the moderator on the site
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 = "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(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.DisplayAlerts = True Application.ScreenUpdating = True End Sub
This comment was minimized by the moderator on the site
Hi please help. I have problem with the code, with this line. Anything wrong? If Rng.Cells(j, 1).Value "" Then
This comment was minimized by the moderator on the site
Hi there. You should have: if Rng.Cells(j, 1).Value = "" then...
This comment was minimized by the moderator on the site
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.
This comment was minimized by the moderator on the site
While runing the above code then shows compile error: syntax error. In the line where "" used and lower line of this.
This comment was minimized by the moderator on the site
I too encounter this issue with this line. If Rng.Cells(j, 1).Value "" Then Can someone help?
This comment was minimized by the moderator on the site
hello, How can I put range automatically without user input
This comment was minimized by the moderator on the site
I'm sorry but I got somebody else to do the scripting for me, I have no knowledge to help you with the modifications.
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Hi, Can some one instruct with reverse engineering - demarging cells with populating same value for all.
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
if you really want to filter then, merging the cells wont help you out.
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
This was really helpful and has saved my time to greater extent
This comment was minimized by the moderator on the site
Can the VBA code be amended to achieve the same for merging across columns (as opposed to down rows, as above) and then repeat for all rows?
This comment was minimized by the moderator on the site
Use the above code and then transpose the result
This comment was minimized by the moderator on the site
Very helpful !! Thanks a lot
This comment was minimized by the moderator on the site
Oh mate, you save lot of my days. Thank you!!!!
This comment was minimized by the moderator on the site
In the above VBA code line number 19 "i=j-1 "
how is it going to affect our logic anyway? I did remove that and could still able to get the same result!
Any specific purpose why it is present?
This comment was minimized by the moderator on the site
It is to limit the value i to last row.
Please disregard this post!
There are no comments posted here yet
Load More
Leave your comments
Posting as Guest
×
Rate this post:
0  Characters
Suggested Locations