Hi,
I have two columns: one with IDs and one with the number of Unique ID (0 or 1). I would like to create a macro that checks all the IDs, find the duplicates and sum the number of Unique ID for these duplicates. If the sum is different thant 1, I want the Unique ID for the first duplicate to be 1 and for the others duplicates to be 0. If there are no duplicates, the unique ID for the ID should be 1.
For the moment the code will do the following:
APID Unique ID
ID 1 1
ID 1 1
ID 1 1
ID 1 0
ID 2 1
ID 3 1
ID 4 1
ID 4 1
ID 4 0
And I want it to do:
APID Unique ID
ID 1 1
ID 1 0
ID 1 0
ID 1 0
ID 2 1
ID 3 1
ID 4 1
ID 4 0
ID 4 0
Any clue? Thanks for your help
Sub findDuplicates()
Dim i As Integer
Dim j As Integer
Dim APID As Integer
Dim UniqueAP As Integer
Dim Lastrow
Lastrow = Sheets("Open APs").Cells(Rows.Count, 1).End(xlUp).Row
APID = 38
UniqueAP = 39
j = 2
For i = 5 To Lastrow
If Sheets("Open APs").Cells(i, APID).Value = Sheets("Open APs").Cells(i + 1, APID).Value And Sheets("Open APs").Cells(i, UniqueAP).Value = 0 _
And Sheets("Open APs").Cells(i + 1, UniqueAP).Value = 0 Then
Sheets("Open APs").Cells(i, UniqueAP).Value = 1
ElseIf Sheets("Open APs").Cells(i, APID).Value = Sheets("Open APs").Cells(i + 1, APID).Value And Sheets("Open APs").Cells(i, UniqueAP).Value = 1 _
And Sheets("Open APs").Cells(i + 1, UniqueAP).Value = 1 Then
Sheets("Open APs").Cells(i + 1, UniqueAP).Value = 0
ElseIf Sheets("Open APs").Cells(i, APID).Value = Sheets("Open APs").Cells(i + 1, APID).Value And _
Sheets("Open APs").Cells(i, APID).Value = Sheets("Open APs").Cells(i + j, APID).Value Then
j = j + 1
If Application.Sum(Range(Cells(i, UniqueAP), Cells(j, UniqueAP))) <> 1 Then
Sheets("Open APs").Cells(i, UniqueAP).Value = 1
End If
End If
Next i
End Sub
Bookmarks