Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
elfmajesty
 
Posts: n/a
Default Delete Duplicate Rows Macro Question

Hello. I found this macro to delete duplicate rows from a spreadsheet, which
works great if the duplicates adjacent to each other. Does anyone know a way
to modify it so that I do not have to sort the spreadsheet first to get the
duplicates to be adjacent? I would love to be able to just run it on an
entire spreadsheet "as-is" and have it pick up the dups and delete them.

Thank you. Macro below:

Sub DeleteDupes()
Dim Iloop As Integer
Dim Numrows As Integer

'Turn off warnings, etc.
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Numrows = Range("A65536").End(xlUp).Row
Range("A1:B" & Numrows).Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, _
Key2:=Range("B1"), Order2:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
For Iloop = Numrows To 2 Step -1
If Cells(Iloop, "A") + Cells(Iloop, "B") = Cells(Iloop - 1, "A") + _
Cells(Iloop - 1, "B") Then
Rows(Iloop).Delete
End If
Next Iloop
'Turn on warnings, etc.
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Delete Duplicate Rows Macro Question

I think sorting the data may even make the macro work faster.

But this worked ok when I checked a single column:

Option Explicit
Sub DeleteDupes()
Dim Iloop As Long
Dim Numrows As Long

'Turn off warnings, etc.
Application.ScreenUpdating = False
Application.DisplayAlerts = False

With ActiveSheet
Numrows = .Range("A65536").End(xlUp).Row

For Iloop = Numrows To 2 Step -1
If Application.CountIf(.Range("a1:A" & Iloop - 1), _
.Cells(Iloop, "A").Value) 0 Then
'there's at least one duplicate above this row
.Rows(Iloop).Delete
End If
Next Iloop
End With
'Turn on warnings, etc.
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

If I wanted to check a pair of columns (A concatenated with B or A+B (if they're
numeric), then I'd insert a helper column and stick a formula in that cell that
adds them or concatentates them nicely:

=a1+b1
or
=A1&"|"&B1

The vertical bar is used so that
AAA BBB
and
AA ABBB
won't be treated as duplicates.


elfmajesty wrote:

Hello. I found this macro to delete duplicate rows from a spreadsheet, which
works great if the duplicates adjacent to each other. Does anyone know a way
to modify it so that I do not have to sort the spreadsheet first to get the
duplicates to be adjacent? I would love to be able to just run it on an
entire spreadsheet "as-is" and have it pick up the dups and delete them.

Thank you. Macro below:

Sub DeleteDupes()
Dim Iloop As Integer
Dim Numrows As Integer

'Turn off warnings, etc.
Application.ScreenUpdating = False
Application.DisplayAlerts = False

Numrows = Range("A65536").End(xlUp).Row
Range("A1:B" & Numrows).Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, _
Key2:=Range("B1"), Order2:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
For Iloop = Numrows To 2 Step -1
If Cells(Iloop, "A") + Cells(Iloop, "B") = Cells(Iloop - 1, "A") + _
Cells(Iloop - 1, "B") Then
Rows(Iloop).Delete
End If
Next Iloop
'Turn on warnings, etc.
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete row where there is duplicate data in Column E SITCFanTN New Users to Excel 1 June 4th 06 09:35 AM
Delete rows with duplicate values Smohrman Excel Worksheet Functions 14 May 5th 06 12:20 PM
How do I delete both duplicate rows from a spreadsheet? natalia Excel Discussion (Misc queries) 3 April 29th 06 11:40 PM
Macro Help In Excel welshlad Excel Discussion (Misc queries) 14 October 26th 05 02:34 PM
Keeping duplicate rows Daniell Excel Worksheet Functions 2 April 18th 05 06:56 AM


All times are GMT +1. The time now is 07:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"