Remove duplicate rows

ADAMC

Well-known Member
Joined
Mar 20, 2007
Messages
1,169
Office Version
  1. 2013
Platform
  1. Windows
Hi all,

Can someone help with me with a macro to remove duplicate rows based on an input in column E?
Heres some sample data below:

Excel Workbook
ABCDEFGH
3NameDbJob NumberAddessExchangeRequired DateSearch ResultsComments
4blahblahblahblahMYP35222*24/08/2011*blahblah
5blahblahblahblahMYP35222*24/08/2011*blahblah
6blahblahblahblahMYP35222*24/08/2011*blahblah
7blahblahblahblahMYP35222*24/08/2011*blahblah
8blahblahblahblahMYP35222*24/08/2011*blahblah
create_report 1


Basically I need to be left with only 1 row of data where duplicate code appears in column E. In the example above there is a header row which starts in row 3. Iif i run the macro on the sample above i should be left with a header row plus 1 row as the other 4 are duplicates. The start row (row 3) is always the same but the data set can change to a different end row day on day so i need the macro to run until it hits the last row.

Thanks for any help with this.

Edit** the screenshot above shows that the cells contain a * at the end of the reference number i need to remove duplicates in my actual spreadsheet there is no * not sure how this got there.

Thanks
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try

Code:
Sub NoDup()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = LR To 4 Step -1
    If WorksheetFunction.CountIf(Range("E4:E" & i), Range("E" & i).Value) > 1 Then Rows(i).Delete
Next i
End Sub
 
Upvote 0
Try this...

Code:
Sub Delete_Duplicate_Rows( )
Dim i as Long
Dim LR as Long

LR = Range("E" & rows.count).End(xlUp).row

For i = LR to 4 Step -1
   If Range("E" & i) = Range("E" & i - 1) then
        Range("E" & i).EntireRow.Delete
   End If
Next i
End Sub
 
Upvote 0
Thanks to you both for your replies....both scenarios work....

I tested using VOG's code:

Code:
Sub NoDup()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = LR To 4 Step -1
    If WorksheetFunction.CountIf(Range("E4:E" & i), Range("E" & i).Value) > 1 Then Rows(i).Delete
Next i
End Sub

Problem I have is some are merged cells so when I unmerge and run it leaves me with a lot of blank rows....could you please adapt so blank rows are also deleted? If this could be added this would be perfect for what I need :)

Thank you :)
 
Upvote 0
Try

Code:
Sub NoDup()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = LR To 4 Step -1
    If WorksheetFunction.CountIf(Range("E4:E" & i), Range("E" & i).Value) > 1 Then Rows(i).Delete
Next i
On Error Resume Next
Columns("E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 
Upvote 0
Thanks to you both for your replies....both scenarios work....

I tested using VOG's code:

Code:
Sub NoDup()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = LR To 4 Step -1
    If WorksheetFunction.CountIf(Range("E4:E" & i), Range("E" & i).Value) > 1 Then Rows(i).Delete
Next i
End Sub
Problem I have is some are merged cells so when I unmerge and run it leaves me with a lot of blank rows....could you please adapt so blank rows are also deleted? If this could be added this would be perfect for what I need :)

Thank you :)

use the following...

Code:
Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
 
Upvote 0
Works great on my sample data....Really appreciate the response massive time saver for me.

cant thank you both enough! :)
 
Upvote 0
Hello all :)

after such success with my initial post i have found other ways i can improve my spreadsheet just need the help :)

the macro to remove duplicates works perfectly.....another great help would be if after the duplicate macro is run i could do the following:

Insert a new column at the beginning of the sheet and create a new header row in row 3 titled 'ref'
then for every row:
copy the job number in column C but remove the last 2 numbers
paste this ref into the new column (Column A)
and add the 2 letters from each row of column B to the end of the ref just pasted to column A

So from this:

Excel Workbook
ABCDEFGH
3Cst NameDbJob NumberAddessExchangeRequired DateSearch ResultsComments
4BLAHMY*RMD80708*BLAHMYP35222*BLAHBLAHBLAH
5BLAHMY*RMD81708*BLAHMYP35222*BLAHBLAHBLAH
6BLAHMY*RMD82808*BLAHMYP35222*BLAHBLAHBLAH
7BLAHMY*RMD88006*BLAHMYP35222*BLAHBLAHBLAH
8BLAHMY*RMD90008*BLAHMYP35222*BLAHBLAHBLAH
create_report 1



To this:

Excel Workbook
ABCDEFGHI
3REFCst NameDbJob NumberAddessExchangeRequired DateSearch ResultsComments
4RMD807MYBLAHMY*RMD80708*BLAHMYP35222*BLAHBLAHBLAH
5RMD817MYBLAHMY*RMD81708*BLAHMYP35222*BLAHBLAHBLAH
6RMD828MYBLAHMY*RMD82808*BLAHMYP35222*BLAHBLAHBLAH
7RMD880MYBLAHMY*RMD88006*BLAHMYP35222*BLAHBLAHBLAH
8RMD900MYBLAHMY*RMD90008*BLAHMYP35222*BLAHBLAHBLAH
create_report 1




I hope this makes sense..........please post back if this isnt clear enough thanks for any help.
 
Upvote 0
Untested so try with a copy of your sheet

Code:
Sub NoDup()
Dim LR As Long, i As Long
LR = Range("E" & Rows.Count).End(xlUp).Row
For i = LR To 4 Step -1
    If WorksheetFunction.CountIf(Range("E4:E" & i), Range("E" & i).Value) > 1 Then Rows(i).Delete
Next i
On Error Resume Next
Columns("E").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
LR = Range("E" & Rows.Count).End(xlUp).Row
Columns("A").Insert
Range("A3").Value = "Cst Name"
For i = 4 To LR
    Range("A" & i).Value = Left(Range("D" & i).Value, Len(Range("D" & i).Value) - 2)
Next i
End Sub
 
Upvote 0
Hi VOG

thanks for the reply...this doesnt seem to quite work but as i cant understand your code i cant see what the problem is...heres a before and after....

before:

Excel Workbook
ABCDEFGH
3Cst NameDbJob NumberAddessExchangeRequired DateSearch ResultsComments
4blahMY*RMD80708*blahMYP35222*blahblahblah
5blahMY*RMD81708*blahMYP35222*blahblahblah
6blahMY*RMD82808*blahMYP35222*blahblahblah
7blahMY*RMD88006*blahMYP35222*blahblahblah
8blahMY*RMD90008*blahMYP35222*blahblahblah
9blahMY*RMX41801*blahMYP35222*blahblahblah
create_report 1


AFTER:

Excel Workbook
ABCDEFGHI
3REFCst NameDbJob NumberAddessExchangeRequired DateSearch ResultsComments
4RMD8070blahMY*RMD80708*blahMYP35222*blahblahblah
create_report 1


As you can see it removes all the duplicates correctly but column A should be:
RMD807MY
I cant figure where it gets the 'zero' on the end from.

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,834
Members
452,947
Latest member
Gerry_F

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