VBA Delete Both Duplicates Entire Row

Coater

New Member
Joined
Apr 1, 2021
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
I need a VBA code to delete all duplicates numbers and only leave what is not a duplicate number.

An example is 1, 2, 3, 4, 5, 5, 4, 3. The result will be 1, 2
 
Thanks you! The additional information now clarifies the issues raised. (y)


In terms of Excel worksheets, that is a very small number.

Try this with a copy of your workbook.
Set the column that has the (possibly) duplicate numbers in the 'Const' line near the start of the code.

VBA Code:
Sub DeleteDupeRows()
  Dim rCrit As Range
  Dim LastRow As Long

  Const DupesCol As String = "I"  '<- Column that your numbers of interest are in

  Application.ScreenUpdating = False
  LastRow = Range(DupesCol & Rows.Count).End(xlUp).Row
  Set rCrit = Range("ZZ1:ZZ2")
  rCrit.Cells(2).Formula = Replace("=COUNTIF(#$2:#$" & LastRow & ",#2)>1", "#", DupesCol)
  With Range(Replace("#1:#" & LastRow, "#", DupesCol))
    .AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=rCrit, Unique:=False
    .Offset(1).SpecialCells(xlVisible).EntireRow.Delete
  End With
  If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
  rCrit.ClearContents
  Application.ScreenUpdating = True
End Sub
Hello, I ran this code on 8,731 rows of numbers. The final result was 2,844. The correct number of rows should have deleted 471 resulting in a final 8,260 rows of unique numbers. The formula works fine with a 100 rows of numbers. Do I need to change something. Thanks again for all of your help.
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hello,

I verified the program results by hand and with numerous other data numbers. All were correct. Thank you once again.
 
Upvote 0
Glad it seems to be resolved. (y)

Are you saying that the code from post #10 did work or that you found another way?
 
Upvote 0

Forum statistics

Threads
1,214,838
Messages
6,121,885
Members
449,057
Latest member
Moo4247

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