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

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,589
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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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