VBA Code to delete duplicate records excluding records beginning with an Alpha Character

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,601
Office Version
  1. 2021
Platform
  1. Windows
I have a spreadsheet that contains duplicate references in Col H. I would like VBA code that will retain either the first or second duplicate, except where the reference begins with an Alpha Character.All references beginning with an Alpha character must be retained

In the example below all the references beginning with an Alpha for eg F14890 must be retained. One of the rows containing the duplicate references for eg 203432/1 must be deleted and the other one retained i.e one unique record must be retained

Your assistance in this regard will be most appreciated

Duplicate Records.xlsx
ABCDEFGH
2610-03-2011INVPARTSORDER30I00223-6,199.32203434
2710-03-2011PMTPARTSORDER453,208.67F14890
2810-03-2011PMTPARTSORDER27,118.04F14890
2910-03-2011PMTPARTSORDER27,118.04F14890
3010-03-2011INVPARTSORDER30I00225-729.50203432/1
3110-03-2011INVPARTSORDER30I00222-1,286.83203433/1
3210-12-2010JDRPARTSORDER84248,513.928424
3310-12-2010JDRPARTSORDER84248,513.928424
3411-02-2011INVPARTSORDER30I00080-26,243.49199979
3511-02-2011INVPARTSORDER30I00080-26,243.49199979
3611-02-2011INVPARTSORDER30I00079-6,985.30199980
3711-02-2011INVPARTSORDER30I00079-6,985.30199980
3811-02-2011INVPARTSORDER30I00015-519.16199981
3911-02-2011INVPARTSORDER30I00015-519.16199981
Sheet1
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
It's bedtime so I haven't time to check this. Try it on a COPY of your workbook.
Code:
Option Compare Text
Sub RemoveNonAlphaDups()
Dim rng As Range
Dim lRw As Long, delRws As Range
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
lRw = Range("H" & Rows.Count).End(xlUp).Row
Set rng = Range("H2:H" & lRw)
For i = rng.Rows.Count To 1 Step -1
    If Not (rng.Cells(i).Value Like "[A-Z]*") Then
        If WorksheetFunction.CountIf(rng, rng.Cells(i).Value) > 1 Then
            rng.Cells(i).EntireRow.Delete
        End If
    End If
Next i
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With

End Sub
 
Upvote 0
Hi JoeMo

Thanks for the help. The code works well, except that it also removes duplicates beginning with an Alpha Character for Eg F14890. All refrences begiining with an Alpha character must be left intact

It would be appreciated if you would amend your code to accomodate this
 
Upvote 0
Hi JoeMo

Thanks for the help. The code works well, except that it also removes duplicates beginning with an Alpha Character for Eg F14890. All refrences begiining with an Alpha character must be left intact

It would be appreciated if you would amend your code to accomodate this
In my tests it does not remove ANY data that begin with an alpha so I'm suspecting that your data may have a space in front of the alpha. I've modified the code so it will remove leading and trailing spaces.
Code:
Sub RemoveNonAlphaDups()
Dim rng As Range
Dim lRw As Long, delRws As Range
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
lRw = Range("H" & Rows.Count).End(xlUp).Row
Set rng = Range("H2:H" & lRw)
For i = rng.Rows.Count To 1 Step -1
    If Not (Trim(rng.Cells(i)) Like "[A-Z]*") Then
        If WorksheetFunction.CountIf(rng, rng.Cells(i).Value) > 1 Then
            rng.Cells(i).EntireRow.Delete
        End If
    End If
Next i
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With

End Sub
 
Upvote 0
Hi JoeMo

Thanks for the help. It is working perfectly. I need one more amendment to your code. I import the data and for some unkown reason there is a ref 204281-/ as well as a refrence as a ref 204281/* which your code picks up as a duplicate and deletes the record 204281/*

Please amend your code according as the duplicate references must be an exact match, before one of the duplicates are deleted

Your assistance in this regard will be most appreciated
 
Upvote 0
Hi JoeMo

Thanks for the help. It is working perfectly. I need one more amendment to your code. I import the data and for some unkown reason there is a ref 204281-/ as well as a refrence as a ref 204281/* which your code picks up as a duplicate and deletes the record 204281/*

Please amend your code according as the duplicate references must be an exact match, before one of the duplicates are deleted

Your assistance in this regard will be most appreciated
Howard,
I copied these two refs directly from your post and do not find that either is deleted from a test list containing both. The only help I can offer at this point is if you will PM me, I will send you an email address for you to send me a test workbook with those two data entries copied directly from your imported data. Again, I suspect there may be some hidden characters in the imported data that make those two entries appear to be identical to Excel even though, visually, they are not.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,278
Members
452,902
Latest member
Knuddeluff

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