Remove duplicates in all rows

JadonR

Board Regular
Joined
Apr 15, 2009
Messages
154
Office Version
  1. 365
I am trying to use a macro to delete all duplicate rows. I found the below in a google search but it definitely does not delete all rows.

VBA Code:
Sub Delete_Duplicate_Rows_Based_on_All_Columns()
Dim MyRng As Range
Dim MainColumns As Variant
Set MyRng = Range("A2:C1000")
AllColumns = MyRng.Columns.Count
ReDim MainColumns(0 To AllColumns - 1)
For i = 0 To AllColumns - 1
MainColumns(i) = i + 1
Next i
MyRng.RemoveDuplicates Columns:=(MainColumns), Header:=xlYes
End Sub

For example, I have the below table and there are quite a lot of duplicates.

ABC
ITEM_NODIM_2_UPPERDIM_2
38518SS
38518MM
38518LL
38518XLXL
38518XXLXXL
38518SS
38518SS
38518MM
38518LL
38518XLXL
38504XXLXXL
38504SS
38504MM
38504LL
38504XLXL
38504XXLXXL
38504SS
38504MM
38504LL
38504XLXL
38504XXLXXL
38504XSXS
38504SS
38504MM
38504LL
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi @JadonR, Thanks for posting on the forum.

Try this:
VBA Code:
Sub Delete_Duplicate_Rows_Based_on_All_Columns()
  Dim dic As Object
  Dim rng As Range
  Dim i As Long
  
  Set dic = CreateObject("Scripting.Dictionary")
  Set rng = Range("A2:C" & Range("A" & Rows.Count).End(3).Row)
  For i = 1 To rng.Columns.Count
    dic(i) = i
  Next i
  rng.RemoveDuplicates Columns:=dic.keys, Header:=xlYes
End Sub

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
When I run it, some duplicate rows are removed but most are left. But then I realized that part of the reason is because each of these cells are pulling data from a vlookup.

1680520409761.png


So I copied and pasted the data into a new sheet and chose to paste values only. When I ran it again, it missed row 8. I believe this is because this value was duplicated 3 times instead of just two.

ABC
ITEM_NODIM_2_UPPERDIM_2
38518SS
38518MM
38518LL
38518XLXL
38518XXLXXL
38518SS
38504SS
38504MM
38504LL
38504XLXL
38504XXLXXL

Can you modify the macro to remove rows where there are more than two duplicates?
 
Upvote 0
Actually, this seems to do it.

VBA Code:
Sub Delete_Duplicate_Rows_Based_on_All_Columns()

    With ActiveSheet
        Set rng = Range("A1", Range("C1").End(xlDown))
        rng.RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
    End With

End Sub

Could I just use this?
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

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