Vba to delete entire row of duplicate and original data entries

GID1

New Member
Joined
Oct 11, 2022
Messages
23
Office Version
  1. 2021
Platform
  1. Windows
Hi

Can anyone help me out . I am having a hard time on this one. I would like to delete of entire row of the duplicate and original in column E whenever i enter duplicate in the Column E. Please kindly see the attached image. In the image i have two tables top is before and bottom table is after deleting the entire row of the duplicate and original in column E.


Thank you.

GD
 

Attachments

  • Sample.jpg
    Sample.jpg
    171.6 KB · Views: 15

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
The below code below assumes your Table starts in A1, that the sheet you want to use is the active sheet, and that you are only looking at Column E to determine if something is a duplicate or not.

An XL2BB of the test data:
20221016 VBA Remove Duplicates GID1.xlsm
ABCDEFG
1LocationPart NumberDescriptionCodeSerial NumberDateRemarks
21AB1BBook1231A212/10/2022Taken
31AB2BPencil1241A313/10/2022Taken
41AB1BEraser1251A114/10/2022Taken
51AB4BRuler1261A415/10/2022Taken
61AB1BBook1231A212/10/2022Taken
Data


VBA Code:
Sub RemoveDuplicates()

    Dim shtData As Worksheet, shtRpt As Worksheet
    Dim rngData As Range, arrData As Variant
    Dim lrowData As Long, lcolData As Long, nxtColData As Long
    Dim rngRptInclHdg As Range, rngRpt As Range, arrRpt As Variant
    Dim dictData As Object, dictKey As String
    Dim i As Long, NoOfRows As Long

    Set shtData = ActiveSheet
 
    With shtData
        lrowData = .Cells(Rows.Count, "E").End(xlUp).Row
        lcolData = .Cells(1, Columns.Count).End(xlToLeft).Column
        nxtColData = lcolData + 1
        Set rngData = .Range(.Cells(1, "A"), .Cells(lrowData, nxtColData))
        arrData = rngData.Value
    End With
 
    Set dictData = CreateObject("Scripting.dictionary")
 
    ' Load details into Dictionary and flag duplicates
    For i = 1 To UBound(arrData)
        dictKey = arrData(i, 5)
        If Not dictData.exists(dictKey) Then
            dictData(dictKey) = i
        Else
            arrData(i, nxtColData) = 1
            arrData(dictData(dictKey), nxtColData) = 1        ' Could test for already set to 1 but no real added value
        End If
    Next i
 
    rngData.Columns(lcolData + 1) = Application.Index(arrData, 0, lcolData + 1)
 
    ' Delete duplicate rows
    NoOfRows = Application.Count(rngData.Columns(nxtColData))
    If NoOfRows > 0 Then
        Application.ScreenUpdating = False
        With rngData.Resize(, nxtColData)
          .Sort Key1:=.Columns(nxtColData), Order1:=xlAscending, Header:=xlNo
          .Resize(NoOfRows).EntireRow.Delete
        End With
        Application.ScreenUpdating = True
  End If
 
End Sub
 
Last edited:
Upvote 0
The below code below assumes your Table starts in A1, that the sheet you want to use is the active sheet, and that you are only looking at Column E to determine if something is a duplicate or not.

An XL2BB of the test data:
20221016 VBA Remove Duplicates GID1.xlsm
ABCDEFG
1LocationPart NumberDescriptionCodeSerial NumberDateRemarks
21AB1BBook1231A212/10/2022Taken
31AB2BPencil1241A313/10/2022Taken
41AB1BEraser1251A114/10/2022Taken
51AB4BRuler1261A415/10/2022Taken
61AB1BBook1231A212/10/2022Taken
Data


VBA Code:
Sub RemoveDuplicates()

    Dim shtData As Worksheet, shtRpt As Worksheet
    Dim rngData As Range, arrData As Variant
    Dim lrowData As Long, lcolData As Long, nxtColData As Long
    Dim rngRptInclHdg As Range, rngRpt As Range, arrRpt As Variant
    Dim dictData As Object, dictKey As String
    Dim i As Long, NoOfRows As Long

    Set shtData = ActiveSheet
 
    With shtData
        lrowData = .Cells(Rows.Count, "E").End(xlUp).Row
        lcolData = .Cells(1, Columns.Count).End(xlToLeft).Column
        nxtColData = lcolData + 1
        Set rngData = .Range(.Cells(1, "A"), .Cells(lrowData, nxtColData))
        arrData = rngData.Value
    End With
 
    Set dictData = CreateObject("Scripting.dictionary")
 
    ' Load details into Dictionary and flag duplicates
    For i = 1 To UBound(arrData)
        dictKey = arrData(i, 5)
        If Not dictData.exists(dictKey) Then
            dictData(dictKey) = i
        Else
            arrData(i, nxtColData) = 1
            arrData(dictData(dictKey), nxtColData) = 1        ' Could test for already set to 1 but no real added value
        End If
    Next i
 
    rngData.Columns(lcolData + 1) = Application.Index(arrData, 0, lcolData + 1)
 
    ' Delete duplicate rows
    NoOfRows = Application.Count(rngData.Columns(nxtColData))
    If NoOfRows > 0 Then
[/QUOTE]








[QUOTE="Alex Blakenburg, post: 5962486, member: 473943"]
        Application.ScreenUpdating = False
        With rngData.Resize(, nxtColData)
          .Sort Key1:=.Columns(nxtColData), Order1:=xlAscending, Header:=xlNo
          .Resize(NoOfRows).EntireRow.Delete
        End With
        Application.ScreenUpdating = True
  End If
 
End Sub






Hi Alex,

Thank you so much for ur reply. It does delete the entire rows of duplicates and its original in Column E, but there is one issue. In Row One , the text from the head drop down and fill in the below cells.
Please take a look at the image. Could u please advise me what could be done.
Thank you so much for ur help.
 

Attachments

  • Error.jpg
    Error.jpg
    134.4 KB · Views: 3
Upvote 0
I am afraid I don't understand what you are trying to say.

Was row 2 there in the data original data ?
Show me before, after and what you are expecting.
 
Upvote 0
Hi

Here is the attachment.


The result becomes like this after the macro run, to record the date i use this code :
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Cells(Target.Row, "M") = Date
Application.EnableEvents = True

End Sub

But the main concern is show below.Please take a look.



LocationPart NumberDescrCodeSerial NumberDateRemarks
1A1Taken
LocationPart NumberDescrCode1A5DateTaken
 
Upvote 0
You did not answer my questions or provide the visibility over your data before, after & expected that I asked for.

There is nothing in the code that will generate your line 3. It looks to me that your source data has the heading repeating in the data. I don't know how it is picking up altered data in the Serial Number and Remarks field.

In nearly all cases macros need to know the Column & Row numbers in which the data resides. All your images and sample do not show Column & Row references.

If you provide the necessary information I can trouble shoot the issue.
 
Upvote 0
This is before i enter duplicate.

Error (version 1).xlsb
ABCDEFG
1LocationPart NumberDescrCodeSerial NumberDateRemarks
21AB1BPencil1241A344850Taken
31AB2BEraser1251A144850Taken
41AB3BRuler1261A444850Taken
51AB4BBook1261A544850Taken
Sheet2




This is after i enter the duplicate serial number "1A3" the table should look like this.

LocationPart NumberDescrCodeSerial NumberDateRemarks
1AB2BEraser1251A110/16/2022Taken
1AB3BRuler1261A410/16/2022Taken
1AB4BBook1261A510/16/2022Taken
[/XD][/XR][/RANGE]
 
Upvote 0
This is the main sheet that i would like to run the macro.


Error.xlsm
ABCDEFG
1LocationPart NumberDescrCodeSerial NumberDateRemarks
21AB1BPencil1241A313/10/2022Taken
31AB2BEraser1251A114/10/2022Taken
41AB3BRuler1261A415/10/2022Taken
51AB4BBook1261A515/10/2023Taken
6     
Stock In-Out
Cell Formulas
RangeFormula
A2:A6A2=IFERROR(INDEX(Inventory!A:A,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
B2:B6B2=IFERROR(INDEX(Inventory!B:B,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
C2:C6C2=IFERROR(INDEX(Inventory!C:C,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
D2:D6D2=IFERROR(INDEX(Inventory!D:D,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
F2:F6F2=IFERROR(INDEX(Inventory!G:G,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")



This is the second sheet that is tracking the inventory in and out. So whenever the serial number is entered in the main sheet
 
Upvote 0
This is the main sheet that i would like to run the macro.


Error.xlsm
ABCDEFG
1LocationPart NumberDescrCodeSerial NumberDateRemarks
21AB1BPencil1241A313/10/2022Taken
31AB2BEraser1251A114/10/2022Taken
41AB3BRuler1261A415/10/2022Taken
51AB4BBook1261A515/10/2023Taken
6     
Stock In-Out
Cell Formulas
RangeFormula
A2:A6A2=IFERROR(INDEX(Inventory!A:A,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
B2:B6B2=IFERROR(INDEX(Inventory!B:B,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
C2:C6C2=IFERROR(INDEX(Inventory!C:C,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
D2:D6D2=IFERROR(INDEX(Inventory!D:D,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
F2:F6F2=IFERROR(INDEX(Inventory!G:G,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")



This is the second sheet that is tracking the inventory in and out. So whenever the serial number is entered in the main sheet
This is the main sheet"Stock In>Out" which i would like to run the macro.


Error.xlsm
ABCDEFG
1LocationPart NumberDescrCodeSerial NumberDateRemarks
21AB1BPencil1241A313/10/2022Taken
31AB2BEraser1251A114/10/2022Taken
41AB3BRuler1261A415/10/2022Taken
51AB4BBook1261A515/10/2023Taken
6     
Stock In-Out
Cell Formulas
RangeFormula
A2:A6A2=IFERROR(INDEX(Inventory!A:A,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
B2:B6B2=IFERROR(INDEX(Inventory!B:B,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
C2:C6C2=IFERROR(INDEX(Inventory!C:C,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
D2:D6D2=IFERROR(INDEX(Inventory!D:D,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")
F2:F6F2=IFERROR(INDEX(Inventory!G:G,MATCH('Stock In-Out'!E2,Inventory!E:E,0)),"")



This is the second sheet that is tracking the inventory in and out. So whenever the serial number is entered in the main sheet"Stock In>Out" , the status column in this sheet will change to "Taken".
as i have enter the formula to do so. So when the duplicates serial number is entered again into the main sheet"Stock In>Out" , i need that duplicates and the original to be deleted off from cell, so that the status column in this sheet will change back to "In Inventory".

Here is the second sheet.

Error (version 2).xlsb
ABCDEFGH
1LocationPart NumberDescrCodeSerial NumberStatusDateRemarks
21AB1BPencil1241A3In Inventory13/10/2022Taken
31AB2BEraser1251A1In Inventory14/10/2022Taken
41AB3BRuler1261A4In Inventory15/10/2022Taken
51AB4BBook1261A5In Inventory15/10/2023Taken
6 
7 
8 
9 
10 
11 
Inventory
Cell Formulas
RangeFormula
H2:H11H2=IFERROR(INDEX('Stock In-Out'!G:G,MATCH(Inventory!E2,'Stock In-Out'!E:E,0)),"")


Pardon me for my english. Thank you for being patience and helping me out on this one.
 
Upvote 0
The information in the last post was perfect.

It is generally not good practice to reference the sheet name of the current sheet in the formula.
Having said that I would not have expected that it would be impacted by a sort but that appears to be the case.

Go to your sheet > Stock In-Out
Press Ctrl+H (replace)
In the find what box copy the following > 'Stock In-Out'!
Leave the replace with Box empty
Replace All

Save the spreadsheet.

Then rerun the macro and tell me what happens.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,812
Messages
6,121,704
Members
449,048
Latest member
81jamesacct

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