Move all the "Duplicate Numbers" from one sheet to another sheet

fahadalambd

New Member
Joined
Sep 16, 2022
Messages
31
Office Version
  1. 2016
Platform
  1. Windows
Hi everyone,

I hope you are doing well.

Please help me to write a VBA script which will move all the "Duplicates" from "Sheet1" to "Sheet2".

NB: Just wanted to inform that the attached pic has very small data range but in my actual data sheet has more than 40,000 rows and there are so many duplicates numbers are present.

Thank you so much everyone. :)

Kind Regards
 

Attachments

  • 1.PNG
    1.PNG
    8.3 KB · Views: 34
Last edited:

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try this:

VBA Code:
Option Explicit
Sub fahadalambd()
    Application.ScreenUpdating = 0
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Worksheets("Sheet1")
    Set Ws2 = Worksheets("Sheet2")
   
    Dim Rng As Range
    Set Rng = Ws1.Cells(1).CurrentRegion
    Rng.Sort Key1:=Ws1.Cells(1), Order1:=1, Header:=1
    Set Rng = Rng.Resize(Rng.Rows.Count + 1, 1)
   
    Dim ArrIn, ArrOut, i As Long
    ArrIn = Rng
    ReDim ArrOut(1 To UBound(ArrIn, 1) - 1, 1 To 1)
   
    For i = 2 To UBound(ArrIn, 1) - 1
        If ArrIn(i, 1) = ArrIn(i - 1, 1) Or ArrIn(i, 1) = ArrIn(i + 1, 1) Then ArrOut(i, 1) = 1
    Next i
    Ws1.Range("D1").Resize(UBound(ArrOut, 1)).Value = ArrOut
   
    Set Rng = Rng.Resize(Rng.Rows.Count - 1, 4)
    Rng.Sort Key1:=Ws1.Cells(1, 4), Order1:=1, Header:=1
    i = WorksheetFunction.Sum(Ws1.Range("D:D"))
    Set Rng = Rng.Resize(i + 1, 3)
   
    With Rng
        .Copy Ws2.Cells(1)
        .Offset(1).Resize(i).EntireRow.Delete
    End With
   
    Ws1.Range("D:D").ClearContents
    Application.ScreenUpdating = 1
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Option Explicit
Sub fahadalambd()
    Application.ScreenUpdating = 0
    Dim Ws1 As Worksheet, Ws2 As Worksheet
    Set Ws1 = Worksheets("Sheet1")
    Set Ws2 = Worksheets("Sheet2")
  
    Dim Rng As Range
    Set Rng = Ws1.Cells(1).CurrentRegion
    Rng.Sort Key1:=Ws1.Cells(1), Order1:=1, Header:=1
    Set Rng = Rng.Resize(Rng.Rows.Count + 1, 1)
  
    Dim ArrIn, ArrOut, i As Long
    ArrIn = Rng
    ReDim ArrOut(1 To UBound(ArrIn, 1) - 1, 1 To 1)
  
    For i = 2 To UBound(ArrIn, 1) - 1
        If ArrIn(i, 1) = ArrIn(i - 1, 1) Or ArrIn(i, 1) = ArrIn(i + 1, 1) Then ArrOut(i, 1) = 1
    Next i
    Ws1.Range("D1").Resize(UBound(ArrOut, 1)).Value = ArrOut
  
    Set Rng = Rng.Resize(Rng.Rows.Count - 1, 4)
    Rng.Sort Key1:=Ws1.Cells(1, 4), Order1:=1, Header:=1
    i = WorksheetFunction.Sum(Ws1.Range("D:D"))
    Set Rng = Rng.Resize(i + 1, 3)
  
    With Rng
        .Copy Ws2.Cells(1)
        .Offset(1).Resize(i).EntireRow.Delete
    End With
  
    Ws1.Range("D:D").ClearContents
    Application.ScreenUpdating = 1
End Sub
Thank you so much mate. The above script is working perfectly in the large data range as well. :) One more question - If possible is there any way to just COPY from "Sheet1" to "Sheet2". Sorry for asking again but it would be really appreciate if you help me. Thanks again for the help
 
Upvote 0

Forum statistics

Threads
1,224,739
Messages
6,180,674
Members
452,993
Latest member
FDARYABEE

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