Copy duplicate rows to another sheet

N013

New Member
Joined
Jul 27, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

Hope you can help me with this issue. I would like to copy an entire row to a new worksheet if there are duplicate ID numbers (see attachment).
So if the ID numbers in column A are equal, I would like to copy the entire row (in this case row 3 and row 4) to a new worksheet. Is this possible?
 

Attachments

  • duplicate.PNG
    duplicate.PNG
    5 KB · Views: 8

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi, welcome to Mr Excel.

Let me know if this works for you ?
thanks
Rob

VBA Code:
Sub copy_row()

Dim lastrow, x, dup_cnt As Long

lastrow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row 'uses col 1 to get last row

For x = 3 To lastrow

    MyID = Range("A" & x).Value
    
    dup_cnt = WorksheetFunction.CountIf(Columns(1), MyID) 'count how many instances there are
    
    If dup_cnt > 1 Then
        Range(Cells(x, 1), Cells(x, 3)).EntireRow.Copy 'copy columns 1 to 3
        nextrow = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row + 1 'uses col 1 to get last row and adds 1
        Sheets("Sheet2").Range("A" & nextrow).EntireRow.PasteSpecial xlPasteValues 'copy columns 1 to 3
    End If

Next x


End Sub
 
Upvote 0
Thank you for your reply! I am not familiar with VBA, so I can't use this solution yet. I will start reading about VBA first I guess :)
 
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,272
Members
449,149
Latest member
mwdbActuary

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