Merging duplicate values

kralin

New Member
Joined
Dec 8, 2023
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
Hello - any assistance with this would be greatly appreciated.

I have the following output in Excel:

ID Contact with Date Length of contact

1 John Smith 01/01/2024 60
1 Joan Smith 01/01/2024 60
1 John Smith 02/01/2024 45

and so on for many IDs and numerous contacts - about 5000 rows of data

My desired output however would be

ID Contact with Date Length of contact

1 John Smith and Joan Smith 01/01/2024 60
1 John Smith 02/01/2024 45



Eg I would like to merge the Contact With value when there is a duplicate value for ID, Date and Length of Contact. I would then like to delete the duplicate row so I am left with distinct contacts.



Any assistance with this would be massively appreciated.


Many thanks
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
If your data starts in cell A1 like the following example:

Dante Amor
ABCD
1IDContactDateLength
21John Smith01/01/202460
31Joan Smith01/01/202460
41John Smith02/01/202445
S1


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

Result:
Dante Amor
ABCD
1IDContactDateLength
21John Smith, Joan Smith01/01/202460
31John Smith02/01/202445
4
S1


🤗
 
Upvote 0
Solution

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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