Note: The other languages of the website are Google-translated. Back to English
Se connecter  \/ 
x
or
x
S'inscrire  \/ 
x

or

Comment fusionner rapidement des lignes adjacentes avec les mêmes données dans Excel?

Supposons que vous ayez une feuille de calcul avec les mêmes données dans les lignes adjacentes et que vous souhaitiez maintenant fusionner les mêmes cellules en une seule cellule, afin que les données soient nettes et belles. Comment fusionner des lignes adjacentes avec les mêmes données rapidement et facilement? Aujourd'hui, je vais vous présenter un moyen rapide de résoudre ce problème.


Fusionner les lignes adjacentes des mêmes données avec le code VBA

Bien sûr, vous pouvez fusionner les mêmes données avec Fusionner et centrer commande, mais s'il y a des centaines de cellules à fusionner, cette méthode prendra du temps. Ainsi, le code VBA suivant peut vous aider à fusionner facilement les mêmes données.

1. Maintenez le ALT + F11 clés, et il ouvre le Microsoft Visual Basic pour applications fenêtre.

2. Cliquez insérer > Moduleet collez la macro suivante dans le Modulefenêtre.

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. Puis appuyez sur F5 pour exécuter ce code, une boîte de dialogue s'affiche à l'écran pour sélectionner une plage avec laquelle travailler. Voir la capture d'écran:

doc fusionner les mêmes cellules 2

4. Puis clique OK, les mêmes données de la colonne A seront fusionnées. Voir la capture d'écran:

doc fusionner les mêmes cellules 1


Fusionner les lignes adjacentes des mêmes données avec Kutools for Excel

Avec la Fusionner les mêmes cellules utilité de Kutools pour Excel, vous pouvez rapidement fusionner les mêmes valeurs dans plusieurs colonnes en un seul clic.

Kutools pour Excel : avec plus de 300 compléments Excel pratiques, essai gratuit sans limitation en 30 jours. 

Après l'installation de Kutools pour Excel, vous pouvez procéder comme suit:

1. Sélectionnez les colonnes pour lesquelles vous souhaitez fusionner les lignes adjacentes avec les mêmes données.

2. Cliquez Kutools > Fusionner et fractionner > Fusionner les mêmes cellules, voir capture d'écran:

3. Et puis les mêmes données dans les colonnes sélectionnées ont été fusionnées dans une cellule. Voir la capture d'écran:

doc fusionner les mêmes cellules 4

Cliquez pour télécharger Kutools pour Excel et un essai gratuit maintenant!

Pour en savoir plus, veuillez visiter ce Fusionner les mêmes cellules fonction.


Démonstration: fusionnez les mêmes cellules dans une cellule ou annulez la fusion pour remplir les valeurs en double:

Kutools pour Excel: avec plus de 300 compléments Excel pratiques, essai gratuit sans limitation dans 30 jours. Téléchargez et essayez gratuitement maintenant!

Les meilleurs outils de productivité de bureau

Kutools for Excel résout la plupart de vos problèmes et augmente votre productivité de 80%

  • Réutilisation: Insérer rapidement formules complexes, graphiques et tout ce que vous avez utilisé auparavant; Crypter les cellules avec mot de passe; Créer une liste de diffusion et envoyer des e-mails ...
  • Barre Super Formula (modifiez facilement plusieurs lignes de texte et de formule); Disposition de lecture (lire et modifier facilement un grand nombre de cellules); Coller dans la plage filtrée...
  • Fusionner les cellules / lignes / colonnes sans perdre de données; Contenu des cellules divisées; Combiner des lignes / colonnes en double... Empêcher les cellules en double; Comparer les gammes...
  • Sélectionnez Dupliquer ou Unique Lignes; Sélectionnez les lignes vides (toutes les cellules sont vides); Super Find et Fuzzy Find dans de nombreux classeurs; Sélection aléatoire ...
  • Copie exacte Plusieurs cellules sans changer la référence de formule; Créer automatiquement des références à plusieurs feuilles; Insérer des puces, Cases à cocher et plus encore ...
  • Extrait du texte, Ajouter du texte, Supprimer par position, Supprimer l'espace; Créer et imprimer des sous-totaux de pagination; Conversion entre le contenu et les commentaires des cellules...
  • Super filtre (enregistrer et appliquer des schémas de filtrage à d'autres feuilles); Tri avancé par mois / semaine / jour, fréquence et plus; Filtre spécial par gras, italique ...
  • Combiner des classeurs et des feuilles de travail; Fusionner les tableaux en fonction des colonnes clés; Diviser les données en plusieurs feuilles; Conversion par lots xls, xlsx et PDF...
  • Plus de 300 fonctionnalités puissantes. Prend en charge Office / Excel 2007-2019 et 365. Prend en charge toutes les langues. Déploiement facile dans votre entreprise ou organisation. Essai gratuit de 30 jours. Garantie de remboursement de 60 jours.
onglet kte 201905

Office Tab apporte une interface à onglets à Office et simplifie considérablement votre travail

  • Activer l'édition et la lecture par onglets dans Word, Excel, PowerPoint, Publisher, Access, Visio et Project.
  • Ouvrez et créez plusieurs documents dans de nouveaux onglets de la même fenêtre, plutôt que dans de nouvelles fenêtres.
  • Augmente votre productivité de 50% et réduit des centaines de clics de souris chaque jour!
bas de cabine
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 · 1 years 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 · 1 years 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 · 2 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 · 3 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 · 3 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 · 5 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 · 5 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 · 8 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 · 7 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 · 5 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"