delete old duplicate items by sorting not loop

Hasson

Active Member
Joined
Apr 8, 2021
Messages
401
Office Version
  1. 2016
Platform
  1. Windows
Hello
I want delete the old data . should keep the last duplicate items (should be by sorting not loop if it's possible) based on column C .
(6).xlsx
ABCD
1ITEMCODEIDQTY
21BS-TA-113QQW-14 L/R CLA14 SS230 EG DF/10012
32BS-TA-117QQW-181 RRSDF BRI234
43BS-TA-113QQW-14 L/R CLA14 SS230 EG DF/100400
54BS-TA-119QQW-20 KV/1**2 CLA20 CV123
65BS-TA-107QQW-8 CLA8 UKI456
76BS-TA-113QQW-14 L/R CLA14 SS230 EG DF/100600
SHEET1

result
(6).xlsx
ABCD
1ITEMCODEIDQTY
22BS-TA-117QQW-181 RRSDF BRI234
34BS-TA-119QQW-20 KV/1**2 CLA20 CV123
45BS-TA-107QQW-8 CLA8 UKI456
56BS-TA-113QQW-14 L/R CLA14 SS230 EG DF/100600
REPORT

my real data are 4000 rows
thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Specific reason why sorting is preferred in your requirement?
 
Upvote 0
Hi Zot ,
as I said
my real data are 4000 rows
as I know sorting makes the code faster especially for huge data that's why I wnat by sorting.

by the way the sorting is choice , the loop not bad ,but I search for the best code .
 
Upvote 0
Try this
VBA Code:
Sub Test()

Dim n As Long, eRow As Long
Dim rngUnion As Range
Dim ws1 As Worksheet, wsReport As Worksheet
Dim dData As Object

Application.ScreenUpdating = False

Set dData = CreateObject("Scripting.Dictionary")

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set wsReport = ActiveWorkbook.Sheets("Report")

eRow = ws1.Cells(Rows.Count, "C").End(xlUp).Row

For n = eRow To 2 Step -1
    If Not dData.Exists(ws1.Range("C" & n).Value) Then
        dData.Add ws1.Range("C" & n).Value, Nothing
    Else
        If Not rngUnion Is Nothing Then
            Set rngUnion = Union(ws1.Range("C" & n), rngUnion)
        Else
            Set rngUnion = ws1.Range("C" & n)
        End If
    End If
Next

rngUnion.EntireRow.Delete

End Sub
 
Upvote 0
thanks .
if I make the sheet Report is active will delete duplicates from sheet1 without show any thing in sheet Report
and if if I make the sheet1 is active delete duplicates from sheet1 without show any thing in sheet Report and gives error
what 's my mistake?
 
Upvote 0
thanks .
if I make the sheet Report is active will delete duplicates from sheet1 without show any thing in sheet Report
and if if I make the sheet1 is active delete duplicates from sheet1 without show any thing in sheet Report and gives error
what 's my mistake?
My fault. I was rushing home. 😁 I was deleting the SHeet1 even though I define sheet Report. I thought you can manage

Here the modified code
VBA Code:
Sub Test()

Dim n As Long, eRow As Long
Dim rngUnion As Range
Dim ws1 As Worksheet, wsReport As Worksheet
Dim dData As Object

Application.ScreenUpdating = False

Set dData = CreateObject("Scripting.Dictionary")

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set wsReport = ActiveWorkbook.Sheets("Report")

ws1.Cells.Copy wsReport.Range("A1")

eRow = ws1.Cells(Rows.Count, "C").End(xlUp).Row

For n = eRow To 2 Step -1
    If Not dData.Exists(wsReport.Range("C" & n).Value) Then
        dData.Add wsReport.Range("C" & n).Value, Nothing
    Else
        If Not rngUnion Is Nothing Then
            Set rngUnion = Union(wsReport.Range("C" & n), rngUnion)
        Else
            Set rngUnion = wsReport.Range("C" & n)
        End If
    End If
Next

rngUnion.EntireRow.Delete

End Sub
 
Upvote 0
it's very awesome !!!
just last thing can I rearrange numbers sequences in column A for each item after delete in sheet report like 1,2,3....
 
Upvote 0
it's very awesome !!!
just last thing can I rearrange numbers sequences in column A for each item after delete in sheet report like 1,2,3....
You can use AutoFill
VBA Code:
Sub Test()

Dim n As Long, eRow As Long
Dim rngUnion As Range
Dim ws1 As Worksheet, wsReport As Worksheet
Dim dData As Object

Application.ScreenUpdating = False

Set dData = CreateObject("Scripting.Dictionary")

Set ws1 = ActiveWorkbook.Sheets("Sheet1")
Set wsReport = ActiveWorkbook.Sheets("Report")

ws1.Cells.Copy wsReport.Range("A1")

eRow = ws1.Cells(Rows.Count, "C").End(xlUp).Row

For n = eRow To 2 Step -1
    If Not dData.Exists(wsReport.Range("C" & n).Value) Then
        dData.Add wsReport.Range("C" & n).Value, Nothing
    Else
        If Not rngUnion Is Nothing Then
            Set rngUnion = Union(wsReport.Range("C" & n), rngUnion)
        Else
            Set rngUnion = wsReport.Range("C" & n)
        End If
    End If
Next

rngUnion.EntireRow.Delete

eRow = wsReport.Cells(Rows.Count, "A").End(xlUp).Row
wsReport.Range("A2") = 1
wsReport.Range("A3") = 2
wsReport.Range("A2:A3").AutoFill wsReport.Range("A2", "A" & eRow), 0

End Sub
 
Upvote 0
Another way using Power Query.

Book1 (version 1).xlsm
ABCD
1ITEMCODEIDQTY
21BS-TA-113QQW-14 L/R CLA14 SS230 EG DF/10012
32BS-TA-117QQW-181 RRSDF BRI234
43BS-TA-113QQW-14 L/R CLA14 SS230 EG DF/100400
54BS-TA-119QQW-20 KV/1**2 CLA20 CV123
65BS-TA-107QQW-8 CLA8 UKI456
76BS-TA-113QQW-14 L/R CLA14 SS230 EG DF/100600
8
9
10ITEMCODEIDQTY
112BS-TA-117QQW-181 RRSDF BRI234
124BS-TA-119QQW-20 KV/1**2 CLA20 CV123
135BS-TA-107QQW-8 CLA8 UKI456
146BS-TA-113QQW-14 L/R CLA14 SS230 EG DF/100600
Sheet6


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"CODE"}, {{"Count", each Table.LastN(_,1), type table [ITEM=number, CODE=text, ID=text, QTY=number]}}),
    Expand = Table.ExpandTableColumn(Group, "Count", {"ITEM", "ID", "QTY"}, {"ITEM", "ID", "QTY"}),
    Reorder = Table.ReorderColumns(Expand,{"ITEM", "CODE", "ID", "QTY"}),
    Sort = Table.Sort(Reorder,{{"ITEM", Order.Ascending}})
in
    Sort
 
Upvote 0
@Zot this is perfect ! if you don't mind just I want adding some array into code to make fast for huge data in the future
thanks again .
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

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