提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何在Excel中快速合并具有相同数据的相邻行?

doc合并相同的单元格1

假设您的工作表在相邻行中具有相同的数据,现在您想将相同的单元格合并到一个单元格中,以便数据看起来整洁美观。 (请参见左边的截图。)如何快速方便地将相邻行与相同数据合并? 今天,我会介绍一些快速解决这个问题的方法。

用VBA代码合并相同数据的相邻行

用Kutools for Excel合并相邻行的相同数据


将相邻数据行的相同数据合并到一个单元格中:

为了将多行相同的数据合并到一个单元格中, Kutools for Excel's 合并相同的单元格 功能可以帮助您尽快解决这项工作。

doc合并相同的单元格6

Kutools for Excel:比200方便的Excel加载项,可以在60天免费试用。 下载并免费试用现在!


用VBA代码合并相同数据的相邻行


当然,你可以合并相同的数据 合并与中心 命令,但如果有数百个单元需要合并,则此方法将非常耗时。 所以下面的VBA代码可以帮助您轻松地合并相同的数据。

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic for Applications 窗口。

2。 点击 插页 > 模块,并粘贴在下面的宏 模块窗口。

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。 然后按 F5 键运行此代码,屏幕上会显示一个对话框,用于选择要使用的范围。 看截图:

doc合并相同的单元格2

4. 然后点击 OK,A列中的相同数据将合并在一起。 看截图:

doc合并相同的单元格1


用Kutools for Excel合并相邻行的相同数据

随着 合并相同的单元格 实用程序 Kutools for Excel,您只需点击一下,即可快速合并多列中的相同值。

Kutools for Excel : 与超过300方便的Excel加载项,在60天免费试用没有限制.

安装后 Kutools for Excel,你可以做如下:

1。 选择要合并具有相同数据的相邻行的列。

2。 点击 Kutools > Range > 合并相同的单元格,看截图:

doc合并相同的单元格3

3。 然后,所选列中的相同数据已合并到一个单元格中。 看截图:

doc合并相同的单元格4

注意:如果你想立刻取消合并的单元格, 取消合并单元格 功能可以帮你一个忙,如下图所示:

doc合并相同的单元格5

点击下载Kutools for Excel和免费试用版吧!

要了解更多,请访问此 合并相同的单元格 功能。


演示:将相同单元格合并到一个单元格中或取消合并以填充重复值:

Kutools for Excel:比200方便的Excel加载项,可以在60天免费试用。 下载并免费试用现在!


相关文章:

取消合并单元格并在Excel中填入重复值


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
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.
    Clinton · 4 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.
    Kimberly S · 9 months 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 · 8 months 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 · 11 months 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 · 1 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 · 2 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 · 3 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 · 2 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 · 3 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 · 2 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 · 2 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 · 4 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 · 4 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 · 2 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 · 2 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 · 4 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 · 2 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 · 4 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"