Looking for duplicates and merging data

FatalLordes

Board Regular
Joined
Dec 22, 2017
Messages
76
Office Version
  1. 365
Platform
  1. Windows
Hi all again

So I have a new question. As always, I will try and solve it myself but I always like to post here as, even if I do solve it, normally I'm shown a much better way here which I can then learn from :)

My issue is now that I have duplicate rows which I need to remove BUT I need to merge the data. Below is an example of some data:
1696301788309.png


It needs to look at Column A and if duplicate columns with that same number appears, it needs to merge the data, specifically Column C and G as that is the only two columns that should have different data. I hope my example is clear enough to show what it starts with and what it needs to become. And I need to do this as part of a VBA macro as I'm doing other stuff in VBA to this report as well but I'm stuck on the duplicate row issue.

Any help to point me in the right direction would be greatly appreciated. :)

Thanks in advance.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try below code:

VBA Code:
Option Explicit
Sub combi()
Dim i&, k&, rng, res(), id As String, s1 As String, s2 As String
Dim dic As Object, key, sp
Set dic = CreateObject("Scripting.Dictionary")
rng = Range("A2").CurrentRegion.Value
For i = 1 To UBound(rng)
    id = rng(i, 1) & "|" & rng(i, 2) & "|" & rng(i, 4) & "|" & rng(i, 5) & "|" & rng(i, 6)
    If Not dic.exists(id) Then
        dic.Add id, rng(i, 3) & "|" & rng(i, 7)
    Else
        s1 = Split(dic(id), "|")(0) & IIf(rng(i, 3) = "", "", "," & rng(i, 3))
        s2 = Split(dic(id), "|")(1) & IIf(rng(i, 7) = "", "", "," & rng(i, 7))
        dic(id) = s1 & "|" & s2
    End If
Next
ReDim res(1 To dic.Count, 1 To UBound(rng, 2))
For Each key In dic.keys
    k = k + 1: sp = Split(key, "|")
    res(k, 1) = sp(0): res(k, 2) = sp(1): res(k, 4) = sp(2)
    res(k, 5) = sp(3): res(k, 6) = sp(4)
    sp = Split(dic(key), "|")
    res(k, 3) = IIf(Left(sp(0), 1) = ",", Mid(sp(0), 2, 255), sp(0))
    res(k, 7) = IIf(Left(sp(1), 1) = ",", Mid(sp(1), 2, 255), sp(1))
Next
Range("A10").Resize(UBound(res), UBound(res, 2)).Value = res
End Sub

Test.xlsm
ABCDEFG
1
2913753PEXIP2TEL29/09/2023 13:0029/09/2023 13:15DEF
3913753PEXIP28TEL29/09/2023 13:0029/09/2023 13:15None
4913753PEXIPTEL29/09/2023 13:0029/09/2023 13:15
5913753PEXIP4TEL29/09/2023 13:0029/09/2023 13:15
6912960ICJExt29/09/2023 18:0029/09/2023 18:15
7912960ICJ28Ext29/09/2023 18:0029/09/2023 18:15ABC
8
9
10913753PEXIP2,28,4TEL29/09/2023 13:00:0029/09/2023 13:15:00DEF,None
11912960ICJ28Ext29/09/2023 18:00:0029/09/2023 18:15:00ABC
Sheet1
 
Upvote 0
Wow, that was fast! Thank you, I will try this now! :)
So, I'm thinking I need to remove some lines because I only want the end result to be what is shown in rows 10 and 11 in my sample, which would end up being rows 1 and 2 as I don't need to see any duplicates. But I'm not sure. The sample I gave was just a small sample. The original report may be hundreds if not thousands of rows....
 
Upvote 0

Forum statistics

Threads
1,223,728
Messages
6,174,150
Members
452,548
Latest member
Enice Anaelle

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