Link to home
Start Free TrialLog in
Avatar of Jazz24
Jazz24

asked on

How do i delete a duplicate row by column in an excel spreadsheet?

Hello Experts,

I have 3 columns: First Name, Last Name, and Email

I'm trying to delete an entire row if there is a duplicate email.
Avatar of r_bielak
r_bielak

ASKER CERTIFIED SOLUTION
Avatar of Dave
Dave
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I assumed your duplicate emails are in C Column..and you want to delete just 1 entry of the duplicates..then use the following code..and it will do what you are looking ...
Saurabh...

Sub ddelete()
    Application.ScreenUpdating = False
    Dim i As Long, rng As Range
    Set rng = Range("C1:C" & Cells(65536, "c").End(xlUp).Row)
 
    i = 1
    Do Until i > Cells(65536, "c").End(xlUp).Row
        If Application.WorksheetFunction.CountIf(rng, Cells(i, "C").Value) > 1 Then
            Rows(i).delete
        Else
            i = i + 1
        End If
    Loop
 
    MsgBox "Done"
    Application.ScreenUpdating = True
End Sub

Open in new window

Avatar of Jazz24

ASKER

Thank you that code worked. what if i have multiple worksheets and one to delete worksheet 1 and keep on worksheet 2
This will delete your duplicates from all sheets of your workbook...leaving sheet2
Saurabh...


Sub ddelete()
    Application.ScreenUpdating = False
    Dim i As Long, rng As Range, ws As Worksheet
    Set rng = Range("C1:C" & Cells(65536, "c").End(xlUp).Row)
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Sheet2" Then   'I Assumed you dont want to delete your duplicate emails from sheet2
            ws.Select
            i = 1
 
            Do Until i > Cells(65536, "C").End(xlUp).Row
                If Application.WorksheetFunction.CountIf(rng, Cells(i, "C").Value) > 1 Then
                    Rows(i).delete
                Else
                    i = i + 1
                End If
 
            Loop
        End If
    Next ws
 
 
    MsgBox "Done"
    Application.ScreenUpdating = True
End Sub

Open in new window

The addin will delete duplicate rows between sheets - but as it works left sheet accross, top of sheet down it will delete both intra and inter sheet duplicates rows
Neither of the macros above will delete duplicate rows between sheets (if that was your intent), although they can run on multiple sheets. Can you pls expand on your multiple sheet query?
Cheers
Dave
 
Avatar of Jazz24

ASKER

Thank you! it worked
thx for the grade :)