Excel 2013 Find and copy duplicates to a new worksheet

Nikola Vucica
Nikola Vucica used Ask the Experts™
on
Hello all,

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

https://www.experts-exchange.com/questions/28390015/Excel-2010-Copy-Duplicates-to-a-new-worksheet.html

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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Managing Director/Excel VBA Developer
Distinguished Expert 2018
Commented:
Hi,

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, _
                searchdirection:=xlNext)
                 
                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

Hello
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.

Rgds
gowflow

Author

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.

Thanks
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Hi all,
also thanks gowflow
Regards
If you need my input and to be effective and directed to your request I reiterate: Please post a workbook.
gowflow
ShumsManaging Director/Excel VBA Developer
Distinguished Expert 2018

Commented:
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.

Thanks
Shums

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial