Delete Duplicate Rows from Two Worksheets

wimalik

New Member
Joined
Jul 28, 2020
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
Urgent Help: I get a daily log of data for which I want to run a VBA code to compare data in sheet 1 and sheet 2 and then copy common rows to another sheet, and unique rows to another sheet. Sheet 1 has the correct data, and sheet 2, is where I would like to look and find unique and duplicate rows.

Issue #1: I am unable to successfully do this for 100,000 plus rows as excel crashes.

Issue #2: I am unsure how to create a sheet for unique rows where there is no match.

Issue #3: Does no always copy duplicates.

Could someone help me make changes to this code to help meet it's functionality.


VBA Code:
Sub Duplicate_Rows()
 Dim ur1 As Range, ur2 As Range, dupeRows As Range
    Dim r1 As Range, s1 As String, r2 As Range, s2 As String

    Set ur1 = Worksheets("qry").UsedRange.Rows
    Set ur2 = Worksheets("Dump").UsedRange.Rows  'Find duplicates from Sheet1 in Sheet2

    Set dupeRows = ur2(Worksheets("Dump").UsedRange.Rows.Count + 1)
    For Each r1 In ur1
        s1 = Join(Application.Transpose(Application.Transpose(r1)))
        For Each r2 In ur2
            s2 = Join(Application.Transpose(Application.Transpose(r2)))
            If s1 = s2 Then
                If Intersect(dupeRows, r2) Is Nothing Then
                    Set dupeRows = Union(dupeRows, r2)
                End If
            End If
        Next
    Next

    Dim wb As Workbook, wsDupes As Worksheet    'Move duplicate rows to new Sheet
    Application.ScreenUpdating = False
    Set wb = ThisWorkbook
    Set wsDupes = wb.Worksheets.Add(After:=wb.Worksheets(wb.Worksheets.Count))
    wsDupes.Name = "Dump Duplicates - " & Format(Now, "yyyymmdd-hhmmss")
    dupeRows.Copy
    With wsDupes.Cells(1)
        .PasteSpecial xlPasteAll
        .PasteSpecial xlPasteColumnWidths
        .Select
    End With
    dupeRows.EntireRow.Delete
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Please supply some sample data using XL2BB showing before and after scenarios.
 
Upvote 0
1595955469499.png
 
Upvote 0
(Sheet 1)
EMGeNodeBNameProductNumberProductNameSerialNumberPA_RatingIDID_NAME
AF12AF_YUPAppleIphoneTD3R037
12​
AF12X4
AE13AE_TINYHPLaptopTD3R06
1​
AE13X6
Sheet 2
EMGeNodeBNameProductNumberProductNameSerialNumberPA_RatingIDID_NAME
AF12AF_NOAppleIphoneTD3R037
12​
AF12X4
AE13AE_TINYHPLaptopTD3R06
1​
AE13X6
 
Upvote 0
I have attached sample data. The After isn't doing anything for this
 
Upvote 0
Using Power Query/Get and Transform
1. On the Data Tab, select From Table/Range for each table.
2. With each table in the PQ editor, merge the fields with a delimeter of "|"
3. Join the tables with a left inner join
4. Split the merged table with the same delimeter.
5. Close and Load.

Here are the three Mcodes for the above.

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EMG", type text}, {"eNodeBName", type text}, {"ProductNumber", type text}, {"ProductName", type text}, {"SerialNumber", type text}, {"PA_Rating", type any}, {"ID", type text}, {"ID_NAME", type text}}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Changed Type", {{"PA_Rating", type text}}, "en-US"),{"EMG", "eNodeBName", "ProductNumber", "ProductName", "SerialNumber", "PA_Rating", "ID", "ID_NAME"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Merged")
in
    #"Merged Columns"

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"EMG", type text}, {"eNodeBName", type text}, {"ProductNumber", type text}, {"ProductName", type text}, {"SerialNumber", type text}, {"PA_Rating", type any}, {"ID", type text}, {"ID_NAME", type text}}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Changed Type", {{"PA_Rating", type text}}, "en-US"),{"EMG", "eNodeBName", "ProductNumber", "ProductName", "SerialNumber", "PA_Rating", "ID", "ID_NAME"},Combiner.CombineTextByDelimiter("|", QuoteStyle.None),"Merged")
in
    #"Merged Columns"

End result

Book12
ABCDEFGHIJKLMNOP
1Merged.1Merged.2Merged.3Merged.4Merged.5Merged.6Merged.7Merged.8Table3.Merged.1Table3.Merged.2Table3.Merged.3Table3.Merged.4Table3.Merged.5Table3.Merged.6Table3.Merged.7Table3.Merged.8
2AE13AE_TINYHPLaptopTD3R061AE13X6AE13AE_TINYHPLaptopTD3R061AE13X6
3AF12AF_YUPAppleIphoneTD3R03712AF12X4
Sheet4
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

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