Macro to find where rows start to repeat then delete all rows below

waterkris

New Member
Joined
Jul 26, 2018
Messages
3
Good Day,

I have a spreadsheet which contains names in column A sorted alphabetically. And after a certain row the names start to repeat. I'm trying to write a macro which finds the row where the names start to repeat, then deletes that row and all subsequent rows below. Can anyone help?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Not clear to me how names sorted alphabetically wouldn't have the repeaters grouped together. Can you post some sample data showing exactly how it looks before the macro you want is executed, and how that data will look after the macro is executed?
 
Upvote 0
I have a spreadsheet which contains names in column A sorted alphabetically. And after a certain row the names start to repeat. I'm trying to write a macro which finds the row where the names start to repeat, then deletes that row and all subsequent rows below.
Does this macro do what you want...
Code:
Sub DeleteDupes()
  Columns("A").RemoveDuplicates Columns:=1, Header:=xlNo
End Sub
 
Upvote 0
Good Day All,

Thank you for the suggestions. I apologize for not being very clear on describing the situation.
I'm using a software containing database of crew members of a passenger ship. There is a function to export the crew list to Excel. The names are already sorted alphabetically, but the crew list repeats itself several times. The names are usually around 1200. My wish is after exporting to Excel to run a macro which will find the duplicate names in column A and delete the entire corresponding row. The spread sheet contains about 15-20 columns.
 
Upvote 0
See if this macro does what you want...
Code:
[table="width: 500"]
[tr]
	[td]Sub RemoveDuplicatesAndAssociatedRows()
  Dim LastRow As Long, UnusedCol As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  UnusedCol = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious, , , False).Column + 1
  With Range(Cells(1, UnusedCol), Cells(LastRow, UnusedCol))
    .Formula = "=COUNTIF(A$1:A1,A1)"
    .Value = Evaluate(Replace("IF(@>1,@,"""")", "@", .Address))
    On Error GoTo NoDupes
    .SpecialCells(xlConstants).EntireRow.Delete
  End With
NoDupes:
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Hi Rick,

I tried your code but it deleted everything, all cells.

In fact, now I'm thinking about a different approach. My ultimate goal is to copy the rows which would remain after deleting the duplicate ones. So, what if the macro looks for the first row where the names in column A start to repeat and then copies all rows above this row (except row 1) from column A to F.

.
See if this macro does what you want...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub RemoveDuplicatesAndAssociatedRows()
  Dim LastRow As Long, UnusedCol As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  UnusedCol = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious, , , False).Column + 1
  With Range(Cells(1, UnusedCol), Cells(LastRow, UnusedCol))
    .Formula = "=COUNTIF(A$1:A1,A1)"
    .Value = Evaluate(Replace("IF(@>1,@,"""")", "@", .Address))
    On Error GoTo NoDupes
    .SpecialCells(xlConstants).EntireRow.Delete
  End With
NoDupes:
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Maybe
Code:
Sub CopyUnique()
   Dim Cl As Range, Rng As Range
   
   With CreateObject("scripting.dictionary")
      Set Rng = Range("A2")
      For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then
            .Add Cl.Value, Nothing
            Set Rng = Union(Rng, Cl)
         End If
      Next Cl
   End With
   Rng.EntireRow.Copy Sheets("Sheet2").Range("A2")
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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