Excel 2013 Find and copy duplicates to a new worksheet

Hello all,

Could an expert provide me with VBA to do similar like:


I have a spreadsheet that has an information in Sheet1 and Sheet2. The result must go into Sheet3.

Sheet1 have info in column A (number of rows will change). In this column every cell value is unique.
Sheet2 have info in columns A to F (number of rows will change). Column A may have duplicate values.

If the value of cell A1 of Sheet1 exists in column A of Sheet2, the program will copy that entire row of Sheet2
and paste it in the first free row in the Sheet3. The program must copy every row where he found the same value.

Many thanks in advance.
Nikola VucicaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ShumsExcel & VBA ExpertCommented:

Assuming your Data in Sheet1 & Sheet2 starting from A2:
Sub CompareAndCopy()
    Dim rng As Range
    Dim c As Range
    Dim cfind As Range
    On Error Resume Next
    With ThisWorkbook.Worksheets("Sheet2")
        Set rng = Range(.Range("A2"), .Range("A2").End(xlDown))
        For Each c In rng
            With Worksheets("Sheet1")
                Set cfind = .Columns("A:A").Cells.Find _
                (what:=c.Value, after:=.Cells(1, 1), LookIn:=xlValues, _
                lookat:=xlWhole, searchorder:=xlByRows, _
                If Not cfind Is Nothing Then
                    c.EntireRow.Copy Worksheets("Sheet3"). _
                    Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
                    Set cfind = Nothing
                End If
            End With
        Next c
        Set rng = Nothing
    End With
    Application.CutCopyMode = False
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Could you please post a sample workbook of your data. I see the link you posted refer to a question I participated in providing the solution. when it comes to unique/duplicates issue could be tricky.

Nikola VucicaAuthor Commented:
Hi all,
Shums code works fine. Thanks very much.

Please, can I ask one more question:

Is it possible that we can separate or colour Column A of Sheet1 values if they appeared in column A of Sheet2?

My goal is to mark or separate which values are found and which are not.

Ensure Business Longevity with As-A-Service

Using the as-a-service approach for your business model allows you to grow your revenue stream with new practice areas, without forcing you to part ways with existing clients just because they don’t fit the mold of your new service offerings.

Nikola VucicaAuthor Commented:
Hi all,
also thanks gowflow
If you need my input and to be effective and directed to your request I reiterate: Please post a workbook.
ShumsExcel & VBA ExpertCommented:
Hi Nikola,

That you can do with conditional formatting.

If my code works for your initial question. Then I would request you to close this question and raise another one.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.