Log in
x
or
x
x
Register
x

or

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

doc merge same cells 1

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 left screenshot.) 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


Merge adjacent rows of same data into one cell:

To merge multiple rows of same data into one cell, the Kutools for Excel's Merge Same Cells feature can help you to solve this job as quickly as you can.

doc merge same cells 6

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


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 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 60 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 > Range > Merge Same Cells, see screenshot:

doc merge same cells 3

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

doc merge same cells 4

Note: If you want to unmerge theses merged cells at once, the Unmerge Cell feature can do you a favor as following screenshot shown:

doc merge same cells 5

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 200 handy Excel add-ins, free to try with no limitation in 60 days. Download and free trial Now!


Related article:

Unmerge cells and fill with duplicate values in Excel


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
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Ricardo · 1 months ago
    A formula funciona perfeitamente para valores em colunas, mas se fossem valores para mesclar em linhas? Como seria a formula? Obrigado!!
  • To post as a guest, your comment is unpublished.
    Clinton · 9 months ago
    Thanks a lot for the help. I have a followup question on this. Suppose i have the following situation:

    Apple 2
    Apple 2
    Orange 2
    Orange 2
    Banana 1
    Pear 1
    Kiwi 1

    Running the macro will cause all the '1's and the '2's to be grouped together and my total count will be 3 instead of 7. Is there a way I can merge the cells in the second column based on those in the first? Thanks in advance (:
    • To post as a guest, your comment is unpublished.
      Roberto · 4 months ago
      I have the same problem, I want merge the cells in a column based on the value of another column.. Is there a solution?
  • To post as a guest, your comment is unpublished.
    Kimberly S · 1 years ago
    This is amazing. Thank you so much for the code. Is there any addition that would make it so the segments do not merge over a page break when printing?
    • To post as a guest, your comment is unpublished.
      skyyang · 1 years ago
      Hello, Kimberly,
      I can't get your detailed problem, but, the below VBA code can help you to merge the same cells before and after the page break separately, please try.
      If it helps you, please let me know.

      Sub MergeSameCell_PageBreak()
      Dim Rng As Range, xCell As Range
      Dim xRows As Integer
      Dim xHPB As HPageBreaks
      Dim xChpb As Long
      Dim xBol As Boolean
      Dim xRg As Range
      Set xHPB = ActiveSheet.HPageBreaks
      xChpb = xHPB.Count
      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
      xBol = False
      Set xRg = Rng.Cells(J, 1)
      For xC = 1 To xChpb
      If xRg.Row = xHPB.Item(xC).Location.Row Then
      xBol = True
      Exit For
      End If
      Next
      If xBol Then Exit For
      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
  • To post as a guest, your comment is unpublished.
    Kaushik Vankayala · 1 years ago
    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?
  • To post as a guest, your comment is unpublished.
    Anom Harya · 1 years ago
    Oh mate, you save lot of my days. Thank you!!!!
  • To post as a guest, your comment is unpublished.
    Priya Mohan · 2 years ago
    Very helpful !! Thanks a lot
  • To post as a guest, your comment is unpublished.
    Joel · 2 years ago
    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?
    • To post as a guest, your comment is unpublished.
      Punit · 2 years ago
      Use the above code and then transpose the result
  • To post as a guest, your comment is unpublished.
    anjana anand enginee · 2 years ago
    This was really helpful and has saved my time to greater extent
  • To post as a guest, your comment is unpublished.
    SUMAN PAUL · 2 years ago
    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
  • To post as a guest, your comment is unpublished.
    Xandre · 3 years ago
    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?
    • To post as a guest, your comment is unpublished.
      punit · 2 years ago
      if you really want to filter then, merging the cells wont help you out.
  • To post as a guest, your comment is unpublished.
    Tharaka · 3 years ago
    Hi,

    Can some one instruct with reverse engineering - demarging cells with populating same value for all.
  • To post as a guest, your comment is unpublished.
    PURUSOTHAMAN · 3 years ago
    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
  • To post as a guest, your comment is unpublished.
    PURUSOTHAMAN · 3 years ago
    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
  • To post as a guest, your comment is unpublished.
    Hector Oses · 3 years ago
    I'm sorry but I got somebody else to do the scripting for me, I have no knowledge to help you with the modifications.
  • To post as a guest, your comment is unpublished.
    Debdatta DEY · 3 years ago
    hello,
    How can I put range automatically without user input
  • To post as a guest, your comment is unpublished.
    hasan · 4 years ago
    While runing the above code then shows compile error: syntax error. In the line where "" used and lower line of this.
    • To post as a guest, your comment is unpublished.
      Jimmy · 3 years ago
      I too encounter this issue with this line.

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

      Can someone help?
  • To post as a guest, your comment is unpublished.
    hasan · 4 years ago
    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.
  • To post as a guest, your comment is unpublished.
    Hector Oses · 4 years ago
    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
    • To post as a guest, your comment is unpublished.
      Jimmy · 3 years ago
      Hi please help. I have problem with the code, with this line. Anything wrong?

      If Rng.Cells(j, 1).Value "" Then
      • To post as a guest, your comment is unpublished.
        SonGokussj4 · 3 years ago
        Hi there. You should have:

        if Rng.Cells(j, 1).Value = "" then...
  • To post as a guest, your comment is unpublished.
    Michal · 4 years ago
    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
    • To post as a guest, your comment is unpublished.
      Neo · 4 years ago
      I get same error.


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

      thanks
  • To post as a guest, your comment is unpublished.
    cp · 5 years ago
    If i have same name lie
    Raju 1000
    Raju 2000
    Monu 100
    Monu 200
    Then how can i do marge name with amount
  • To post as a guest, your comment is unpublished.
    Hector · 5 years ago
    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
    • To post as a guest, your comment is unpublished.
      A.Afifi · 3 years ago
      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
    • To post as a guest, your comment is unpublished.
      A.Afifi · 3 years ago
      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
  • To post as a guest, your comment is unpublished.
    Siva · 5 years ago
    Thanks a lot!!! helped me in a crucial time
  • To post as a guest, your comment is unpublished.
    Violeta · 5 years ago
    How do I replicate the VBA macro to merge adjacent cells in columns instead of rows?

    Thanks
    • To post as a guest, your comment is unpublished.
      James · 3 years ago
      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
    • To post as a guest, your comment is unpublished.
      rafael · 5 years ago
      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"