VBA code to delete Duplicate entries

Hiport

Active Member
Joined
May 9, 2008
Messages
455
Hi, i have a spreadsheet which has around 1500 portfolio codes in COL C7, now in this batch of p'folio codes , there are duplicated p'folios codes i.e

Col C
MHLOIU
MHLOIU
MHLOIU
LEHMAN
LEHMAN
LEHMAN

i need a code that will pick out the duplicated p'folios, then
delete them, but at the same time i do not want any blank cells.

Can this be done via VBA?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Something like this would work...
rw1 = first row to be checked (col C)
co1 = column to be checked (col C)

this code is written in such a way that the data range need not be sorted -- ie could have LEHMAN in C2, C3, C1002 and both C3 and C1002 would be removed without needing to be sorted first.

Code:
Sub Delete_Dupes()
Dim rw1 As Long: rw1 = 1
Dim rwx As Long: rwx = rw1
Dim stepx As Integer
Dim co1 As Integer: co1 = 3
Dim bool As Boolean
Do Until Cells(rwx, co1) = ""
    stepx = 1
    If rwx > rw1 Then
        On Error GoTo NewCrit
        bool = IsError(Application.WorksheetFunction.Match(Cells(rwx, co1), Range(Cells(rw1, co1), Cells(rwx - 1, co1)), 0))
        Select Case bool
            Case True
                'no match found thus row = first instance (no delete)
            Case False
                'match found in prior rows therefore duplicate so delete
                Rows(rwx).Delete
                stepx = 0
        End Select
    End If
rwx = rwx + stepx
Loop
Exit Sub

NewCrit:
bool = True
Resume Next
End Sub
 
Last edited:
Upvote 0
Something like this would work...
rw1 = first row to be checked (col C)
co1 = column to be checked (col C)

this code is written in such a way that the data range need not be sorted -- ie could have LEHMAN in C2, C3, C1002 and both C3 and C1002 would be removed without needing to be sorted first.

Code:
Sub Delete_Dupes()
Dim rw1 As Long: rw1 = 1
Dim rwx As Long: rwx = rw1
Dim stepx As Integer
Dim co1 As Integer: co1 = 3
Dim bool As Boolean
Do Until Cells(rwx, co1) = ""
    stepx = 1
    If rwx > rw1 Then
        On Error GoTo NewCrit
        bool = IsError(Application.WorksheetFunction.Match(Cells(rwx, co1), Range(Cells(rw1, co1), Cells(rwx - 1, co1)), 0))
        Select Case bool
            Case True
                'no match found thus row = first instance (no delete)
            Case False
                'match found in prior rows therefore duplicate so delete
                Rows(rwx).Delete
                stepx = 0
        End Select
    End If
rwx = rwx + stepx
Loop
Exit Sub
 
NewCrit:
bool = True
Resume Next
End Sub

Awesome mate, thanx for that, code works perfect
 
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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