Macro to Delete all Duplicates that contain similar text and retain last Duplicate

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have a number of items on Col A on sheet "Outlook reminder" I have wriitten code to delete the first duplicate where the text in Col A is similar (have highlighted those items in Yellow to be deleted 1st duplicate) and retain the final duplicate which is indicated in Red

It would be appreciated if someone could amend my code as it it not delete the items in yellow that contains similar text and retain the final duplicate

Code:
 Sub KeepLastDuplicate()
    Dim lastRow As Long
    Dim i As Long, j As Long
    Dim matchString As String
    Dim matchRow As Long
   
    'Get the last row of data in column A
    lastRow = Sheets("outlook reminders").Cells(Rows.Count, "A").End(xlUp).Row
   
    'Loop through each row of data
    For i = lastRow To 2 Step -1
        'Check if the current row matches the previous row
        If InStr(1, Sheets("outlook reminders").Cells(i - 1, "A").Value, Sheets("outlook reminders").Cells(i, "A").Value, vbTextCompare) > 0 Then
            'If this is the first match in a group of duplicates, store the row number
            If matchString <> Sheets("outlook reminders").Cells(i, "A").Value Then
                matchString = Sheets("outlook reminders").Cells(i, "A").Value
                matchRow = i
            End If
        Else 'No match
            'If there was a previous match, delete all but the last occurrence
            If matchRow > 0 Then
                For j = matchRow To i - 1
                    Sheets("outlook reminders").Rows(j).Delete
                    i = i - 1 'Adjust the counter to account for the deleted row
                    lastRow = lastRow - 1 'Adjust the last row variable to account for the deleted row
                Next j
                matchRow = 0 'Reset the match row variable
            End If
            matchString = "" 'Reset the match string variable
        End If
    Next i
End Sub

See link below contain sample data


I have also posted on Macro to Retain Last Duplicate In Col A where the text is similar
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Thanks for letting us know, but I suggest that you consider the merits of cross-posting. Whilst on the surface you are increasing the pool of potential helpers, you may in fact be decreasing that pool due to the number of helpers in each forum that will not want to bother to check on the other forum if an answer has already been provided and so will just pass over your thread.
 
Upvote 0
I agree fully with you Peter. I posted yesterday on Excel Help Forum as I had not received a response, I decided to post over here as well in the hope I would receive a resoonse. Should I receive a response on either wesbite, I will advise the users
 
Upvote 0
Solution provided by Jindon is perfect

 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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