Compare Lists and Copy Duplicates

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
Hi - I found a few threads on this topic, but I'm struggling. I have 2 lists, Sheets1 Column A and Sheets2 Column B. What i'd like to happen is if the item on Sheets1 appears on Sheets2, then copy the whole row (columns A:E) to Sheets3 A6. I would be running this code mutliple times so if i could get it to copy to next available row would be great. Playing with a bunch of different codes, at some point this stuff will make sense to me.

Thanks!!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try:
Code:
Sub CompareLists()
    Application.ScreenUpdating = False
    Dim Rng As Range, RngList As Object, LastRow As Long
    Set RngList = CreateObject("Scripting.Dictionary")
    For Each Rng In Sheets("Sheet2").Range("B1", Sheets("Sheet2").Range("B" & Sheets("Sheet2").Rows.Count).End(xlUp))
        If Not RngList.Exists(Rng.Value) Then
            RngList.Add Rng.Value, Nothing
        End If
    Next Rng
    For Each Rng In Sheets("Sheet1").Range("A1", Sheets("Sheet1").Range("A" & Sheets("Sheet1").Rows.Count).End(xlUp))
        If RngList.Exists(Rng.Value) Then
            LastRow = Sheets("Sheet3").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
            If LastRow < 6 Then LastRow = 5
            Rng.EntireRow.Copy Sheets("Sheet3").Range("A" & LastRow + 1)
        End If
    Next Rng
    RngList.RemoveAll
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Give this a try. You didn't specify whether you wanted to remove the row from Sheet1 so I didn't do that:

Code:
Public Sub HelpExcel001()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet

Dim lastRow As Long
Dim thisRow As Long
Dim nextRow As Long
Dim foundRow As Variant

Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set ws3 = Sheets("sheet3")

lastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
nextRow = ws3.Cells(ws3.Rows.Count, "A").End(xlUp).Row + 1

For thisRow = 1 To lastRow ' Change 1 to 2 if there's a header
    foundRow = Application.Match(ws1.Cells(thisRow, "A").Value, ws2.Range("B:B"), 0)
    If Not IsError(foundRow) Then
        ws1.Range(ws1.Cells(thisRow, "A"), ws1.Cells(thisRow, "E")).Copy ws3.Cells(nextRow, "A")
        nextRow = nextRow + 1
    End If
Next thisRow

End Sub

WBD
 
Last edited:
Upvote 0
WBD - Works great thanks!!! What would I need to add to remove the items from sheets1 after copying?

Mumps, thank you also!!
 
Upvote 0
Slightly different to remove once copied:

Code:
Public Sub HelpExcel001()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet

Dim lastRow As Long
Dim thisRow As Long
Dim nextRow As Long
Dim foundRow As Variant

Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
Set ws3 = Sheets("sheet3")

lastRow = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
nextRow = ws3.Cells(ws3.Rows.Count, "A").End(xlUp).Row + 1

Application.ScreenUpdating = False

thisRow = 1 ' Change 1 to 2 if there's a header
Do While thisRow <= lastRow
    foundRow = Application.Match(ws1.Cells(thisRow, "A").Value, ws2.Range("B:B"), 0)
    If IsError(foundRow) Then
        thisRow = thisRow + 1
    Else
        ws1.Range(ws1.Cells(thisRow, "A"), ws1.Cells(thisRow, "E")).Copy ws3.Cells(nextRow, "A")
        nextRow = nextRow + 1
        ws1.Cells(thisRow, "A").EntireRow.Delete xlShiftUp
        lastRow = lastRow - 1
    End If
Loop

Application.ScreenUpdating = True

End Sub

WBD
 
Upvote 0
That's great!!! Can I Bug you for another update. Is there a way to reorder the items left on Sheet1 so that there are no blank lines in between items?
 
Upvote 0
It already deletes the entire row when it copies the row to Sheet3. Are there other blank rows in the sheet before this runs?

WBD
 
Upvote 0
There could be items on Sheet1 that aren't on Sheet2. This would leave blank rows where the items that matched were copied to sheet3.
 
Upvote 0
Before:


Book1
ABCDE
1aaaaa
2bbbbb
3ccccc
4ddddd
5eeeee
6fffff
7ggggg
8hhhhh
9iiiii
10jjjjj
Sheet1



Book1
B
1a
2c
3e
4g
5i
Sheet2


After:


Book1
ABCDE
1bbbbb
2ddddd
3fffff
4hhhhh
5jjjjj
Sheet1



Book1
ABCDE
2aaaaa
3ccccc
4eeeee
5ggggg
6iiiii
Sheet3


It removes the row when it's copied to Sheet3.

WBD
 
Upvote 0
My fault!! Sorry!!! I changed the code to take out EntireRow.Delete xlShiftUp and replaced it with ClearContents because I have a validation list in Columns A and G and a formula in Column F.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,314
Members
452,634
Latest member
cpostell

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