Merge duplicated rows

BBCC0000

New Member
Joined
Nov 2, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Does anyone know how to merge duplicated rows by using Excel VBA?

For example, this file has two same ID and Name but the dates are different. I want their dates to be merged, while the ID and Name remain the same. Then delete the other whole duplicated row.

1667967414698.png


The final result would be:

1667967456277.png


Appreciate and thanks to anyone who can help me to create this Excel VBA.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The macro assumes that your data starts in cell A2.

In the last row of the macro:
The result puts it in cell E2 onwards, if you want it in the same cells then change E2 to A2.


Try this:
VBA Code:
Sub Merge_Duplicated()
  Dim a As Variant, b As Variant
  Dim dic As Object
  Dim ky As String
  Dim i As Long, j As Long
  
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("C" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  
  For i = 1 To UBound(a, 1)
    ky = a(i, 2) & "|" & a(i, 3)
    If Not dic.exists(ky) Then
      dic(ky) = dic.Count + 1
    End If
    j = dic(ky)
    b(j, 1) = IIf(b(j, 1) = "", a(i, 1), b(j, 1) & " & " & a(i, 1))
    b(j, 2) = a(i, 2)
    b(j, 3) = a(i, 3)
  Next

  Range("E2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 
Upvote 0
Thank you very much. The code works on me.

Let's say if I have added more columns, which part of code should I change?

Thanks in advance.

1667975006843.png

The macro assumes that your data starts in cell A2.

In the last row of the macro:
The result puts it in cell E2 onwards, if you want it in the same cells then change E2 to A2.


Try this:
VBA Code:
Sub Merge_Duplicated()
  Dim a As Variant, b As Variant
  Dim dic As Object
  Dim ky As String
  Dim i As Long, j As Long
 
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("C" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
 
  For i = 1 To UBound(a, 1)
    ky = a(i, 2) & "|" & a(i, 3)
    If Not dic.exists(ky) Then
      dic(ky) = dic.Count + 1
    End If
    j = dic(ky)
    b(j, 1) = IIf(b(j, 1) = "", a(i, 1), b(j, 1) & " & " & a(i, 1))
    b(j, 2) = a(i, 2)
    b(j, 3) = a(i, 3)
  Next

  Range("E2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 

Attachments

  • 1667974850127.png
    1667974850127.png
    21.4 KB · Views: 8
Upvote 0
Now you must include the birthday, what would be the result?
 
Upvote 0
Try:

VBA Code:
Sub Merge_Duplicated()
  Dim a As Variant, b As Variant
  Dim dic As Object
  Dim ky As String
  Dim i As Long, j As Long, k As Long
  
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2:G" & Range("B" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  
  For i = 1 To UBound(a, 1)
    ky = a(i, 2) & "|" & a(i, 3)
    If Not dic.exists(ky) Then
      dic(ky) = dic.Count + 1
    End If
    j = dic(ky)
    b(j, 1) = IIf(b(j, 1) = "", a(i, 1), b(j, 1) & " & " & a(i, 1))
    For k = 2 To 7
      b(j, k) = a(i, k)
    Next
  Next
  Range("I2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub
 
Upvote 0
Try:

VBA Code:
Sub Merge_Duplicated()
  Dim a As Variant, b As Variant
  Dim dic As Object
  Dim ky As String
  Dim i As Long, j As Long, k As Long
 
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2:G" & Range("B" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
 
  For i = 1 To UBound(a, 1)
    ky = a(i, 2) & "|" & a(i, 3)
    If Not dic.exists(ky) Then
      dic(ky) = dic.Count + 1
    End If
    j = dic(ky)
    b(j, 1) = IIf(b(j, 1) = "", a(i, 1), b(j, 1) & " & " & a(i, 1))
    For k = 2 To 7
      b(j, k) = a(i, k)
    Next
  Next
  Range("I2").Resize(UBound(b, 1), UBound(b, 2)).Value = b
End Sub

Thanks. It has the error 400 when I applied this code to 100+ rows. How do I fix this?

1669189154054.png
 
Upvote 0
Hi
Try
VBA Code:
Sub test()
  Dim a, w
  Dim tx As String
  Dim i&
   a = Range("A2:G" & Cells(Rows.Count, 1).End(xlUp).Row).Value
  With CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a, 1)
    tx = a(i, 2) & a(i, 3)
    If Not .exists(tx) Then
      .Add tx, Array(a(i, 1), a(i, 2), a(i, 3), a(i, 5), a(i, 6), a(i, 7))
      Else
      w = .Item(tx): w(0) = w(0) & " & " & a(i, 1): .Item(tx) = w
    End If
  Next
  Range("I2").Resize(.Count, UBound(a, 2) - 1) = Application.Index(.items, 0, 0)
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,823
Members
449,470
Latest member
Subhash Chand

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top