I have an extremely large data sheet which I need to remove duplicates from a column, however there is different data in corresponding columns, that I need to keep
Example
Record Incident Person
1 101 A
2 201 D
3 301 X
1 102 C
4 401 K
1 101 A
2 202 F
1 101 W
4 401 S
I need to become:
Record Incident Person
1 101 A, W
2 201 D
3 301 X
1 102 C
4 401 K, S
2 202 F
The person column can be separated by a column or be in an additional row, I'm not fussy.
So far I have altered the following macro to work by moving each duplicate to a new worksheet which I have then merged back to the original sheet.
Sub macro()
Dim aIds As Variant
Application.ScreenUpdating = False
ActiveSheet.Copy after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "Temp"
Set origSh = ActiveSheet
Worksheets.Add
Set myRng = Range(origSh.Range("A2"), origSh.Range("A" & Rows.Count).End(xlUp))
myRng.Copy Destination:=ActiveSheet.Range("A1")
ActiveSheet.Range("A:A").RemoveDuplicates Columns:=1, Header:=xlNo
aIds = WorksheetFunction.Index(WorksheetFunction.Transpose(Range(Range("A1"), Range("A" &
Rows.Count).End(xlUp)).Value), 1, 0)
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
Res = 0
For Each Item In aIds
Res = WorksheetFunction.Max(Res, WorksheetFunction.CountIf(myRng, Item))
Next
For Idx = 1 To Res
Worksheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = "Result" & Idx
origSh.Cells(1, "A").EntireRow.Copy ActiveSheet.Range("A1")
For Each Item In aIds
Res1 = 0
On Error Resume Next
Res1 = WorksheetFunction.Match(Item, myRng, 0)
On Error GoTo 0
If Res1 Then
origSh.Cells(Res1 + 1, "A").EntireRow.Copy ActiveSheet.Range("A" & Rows.Count).End(xlUp).Offset(1)
origSh.Cells(Res1 + 1, "A").EntireRow.Delete
End If
Next
Next
Application.DisplayAlerts = False
origSh.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
I'm sure it would be possible to avoid this additional step however I have undertaken a new project with no macros training and am struggling to work it out on my own.