Note: The other languages of the website are Google-translated. Back to English
Inloggen  \/ 
x
or
x
Registreer  \/ 
x

or

Hoe snel aangrenzende rijen met dezelfde gegevens samenvoegen in Excel?

Stel dat u een werkblad heeft met dezelfde gegevens in de aangrenzende rijen, en u wilt nu dezelfde cellen samenvoegen tot één cel, zodat de gegevens er netjes en mooi uitzien. Hoe voeg je aangrenzende rijen met dezelfde gegevens snel en gemakkelijk samen? Vandaag zal ik u een snelle manier voorstellen om dit probleem op te lossen.


Voeg aangrenzende rijen met dezelfde gegevens samen met VBA-code

Natuurlijk kunt u dezelfde gegevens samenvoegen met Samenvoegen en centreren commando, maar als er honderden cellen moeten worden samengevoegd, zal deze methode tijdrovend zijn. Dus de volgende VBA-code kan u helpen dezelfde gegevens eenvoudig samen te voegen.

1. Houd de ALT + F11 toetsen, en het opent de Microsoft Visual Basic voor toepassingen venster.

2. Klikken Invoegen > Moduleen plak de volgende macro in het Modulevenster.

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. Druk vervolgens op F5 toets om deze code uit te voeren, wordt een dialoogvenster op het scherm weergegeven waarin u een bereik kunt selecteren om mee te werken. Zie screenshot:

doc dezelfde cellen samenvoegen 2

4. Dan klikken OK, worden dezelfde gegevens in kolom A samengevoegd. Zie screenshot:

doc dezelfde cellen samenvoegen 1


Voeg aangrenzende rijen met dezelfde gegevens samen met Kutools voor Excel

Met de Voeg dezelfde cellen samen nut van Kutools for Excel, kunt u snel dezelfde waarden in meerdere kolommen samenvoegen met één klik.

Kutools for Excel : met meer dan 300 handige Excel-invoegtoepassingen, gratis te proberen zonder beperking in 30 dagen. 

Na het installeren van Kutools for Excelkunt u het volgende doen:

1. Selecteer de kolommen waarmee u de aangrenzende rijen met dezelfde gegevens wilt samenvoegen.

2. Klikken Kutools > Samenvoegen en splitsen > Voeg dezelfde cellen samen, zie screenshot:

3. En dan zijn dezelfde gegevens in de geselecteerde kolommen samengevoegd in één cel. Zie screenshot:

doc dezelfde cellen samenvoegen 4

Klik om Kutools voor Excel en nu gratis uit te proberen!

Bezoek dit voor meer informatie hierover Voeg dezelfde cellen samen kenmerk.


Demo: voeg dezelfde cellen samen in één cel of maak de samenvoeging ongedaan om dubbele waarden te vullen:

Kutools for Excel: met meer dan 300 handige Excel-invoegtoepassingen, gratis te proberen zonder beperking in 30 dagen. Download en probeer nu gratis!

De beste tools voor kantoorproductiviteit

Kutools voor Excel lost de meeste van uw problemen op en verhoogt uw productiviteit met 80%

  • visfuik: Snel invoegen complexe formules, grafieken en alles wat je eerder hebt gebruikt; Versleutel cellen met wachtwoord; Maak een mailinglijst en stuur e-mails ...
  • Super Formula-balk (bewerk eenvoudig meerdere regels tekst en formule); Lay-out lezen (gemakkelijk grote aantallen cellen lezen en bewerken); Plakken in gefilterd bereik...
  • Voeg cellen / rijen / kolommen samen zonder gegevens te verliezen; Gespleten cellen inhoud; Combineer dubbele rijen / kolommen... Voorkom dubbele cellen; Vergelijk Ranges...
  • Selecteer Dupliceren of Uniek Rijen; Selecteer lege rijen (alle cellen zijn leeg); Super zoeken en fuzzy zoeken in veel werkboeken; Willekeurige selectie ...
  • Exacte kopie Meerdere cellen zonder de formuleverwijzing te wijzigen; Maak automatisch verwijzingen naar meerdere bladen; Plaats kogels, Selectievakjes en meer ...
  • Extraheer tekst, Tekst toevoegen, Verwijderen op positie, Ruimte verwijderen; Paging-subtotalen maken en afdrukken; Converteren tussen celinhoud en opmerkingen...
  • Super filter (bewaar en pas filterschema's toe op andere bladen); Geavanceerd sorteren per maand / week / dag, frequentie en meer; Speciaal filter door vet, cursief ...
  • Combineer werkmappen en werkbladen; Tabellen samenvoegen op basis van sleutelkolommen; Gegevens splitsen in meerdere bladen; Batch Converteer xls, xlsx en PDF...
  • Meer dan 300 krachtige functies. Ondersteunt Office / Excel 2007-2019 en 365. Ondersteunt alle talen. Eenvoudig te implementeren in uw onderneming of organisatie. Gratis proefperiode van 30 dagen met volledige functies. 60 dagen geld-terug-garantie.
kte tabblad 201905

Office-tabblad Brengt een interface met tabbladen naar Office en maakt uw werk veel gemakkelijker

  • Schakel bewerken en lezen met tabbladen in Word, Excel, PowerPoint in, Publisher, Access, Visio en Project.
  • Open en maak meerdere documenten in nieuwe tabbladen van hetzelfde venster in plaats van in nieuwe vensters.
  • Verhoogt uw productiviteit met 50% en vermindert elke dag honderden muisklikken voor u!
officetab onderkant
Say something here...
symbols left.
You are guest
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.
    fahimmulla · 9 months ago
    Hi Guys!
    First of all thank you for all your support. This has been amazing and worked in past. But for some reason it is not working anymore...

    My range at the moment is "$A$2:$A$126551" I am not sure if this was so large before as per user the range was larger in past as well( I am trying to help him out here). Any assistance would be great.

    I get the error:
    "Run-time error '6':

    Overflow"

    on "xRows = WorkRng.Rows.Count"

    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


    Additionally: When I select single date range upto row count 12547 it works but thats only for single date. I am looking to do it for all the dates in the column
  • To post as a guest, your comment is unpublished.
    Fahim · 9 months ago
    Hi,
    this has been amazing and worked in past. But for some reason it is not working anymore...

    My range at the moment is "$A$2:$A$126551" I am not sure if this was so large before as per user the range was larger in past as well( I am trying to help him out here). Any assistance would be great.

    I get the error:
    "Run-time error '6':

    Overflow"

    on "xRows = WorkRng.Rows.Count"

    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
  • To post as a guest, your comment is unpublished.
    Max · 1 years ago
    Thanks a lot for this macro, you saved my day, really!
  • To post as a guest, your comment is unpublished.
    Ricardo · 1 years 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 · 2 years 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 · 2 years 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 · 2 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 · 2 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.
    kaushikbbr · 3 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.
      kaushikbbr · 3 years ago
      It is to limit the value i to last row.
      Please disregard this post!
  • To post as a guest, your comment is unpublished.
    anomharya@gmail.com · 3 years ago
    Oh mate, you save lot of my days. Thank you!!!!
  • To post as a guest, your comment is unpublished.
    Priya Mohan · 4 years ago
    Very helpful !! Thanks a lot
  • To post as a guest, your comment is unpublished.
    Joel · 4 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 · 4 years ago
      Use the above code and then transpose the result
  • To post as a guest, your comment is unpublished.
    anjana anand enginee · 4 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 · 4 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 · 5 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 · 4 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 · 5 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 · 5 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 · 5 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 · 5 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 · 5 years ago
    hello,
    How can I put range automatically without user input
  • To post as a guest, your comment is unpublished.
    hasan · 6 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 · 4 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 · 6 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 · 6 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 · 4 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 · 4 years ago
        Hi there. You should have:

        if Rng.Cells(j, 1).Value = "" then...
  • To post as a guest, your comment is unpublished.
    Michal · 6 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.
      Flick · 5 months ago
      I am getting the same error
    • To post as a guest, your comment is unpublished.
      Neo · 6 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 · 6 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 · 7 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 · 5 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 · 5 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 · 7 years ago
    Thanks a lot!!! helped me in a crucial time
  • To post as a guest, your comment is unpublished.
    Violeta · 7 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 · 4 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 · 7 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"