Delete duplicate rows based on more than one column with excel macro

Shwapx

New Member
Joined
Sep 28, 2022
Messages
48
Office Version
  1. 365
Platform
  1. Windows
I'm trying to create an excel macro that can delete duplicate rows and their original ones based on more than one column.

In the table below you will find the example data and the output for which I'm looking. I need to delete all duplicate rows but only look at some columns like in the example the first 3 headers.

Input table

header1header2header3header4
Test50201
Test50202
Test30203
110204
220305
Test23556
Test23557

Output table after executing the macro

header1header2header3header4
Test30203
110204
220305
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Here is a link to the example file.
The problem is that what you have in column B (Add or Delete) doesn't match what you say in row 13.
Example-sheet.xlsx
ABCDEFGHIJKLMNO
1
2
3Some info about the date
4Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11
5
620210801Addtesttest20000Test23Test44Test55Test66510
720210801Deletetesttest110000Test23Test44Test55Test661020
820210802AddTest23asd5Test11Test11Test11Test1122
920210801DeleteTest23asd5Test11Test11Test11Test1133
1020210803DeleteTest1asdasdTest11Test12Test13Test1112
11
12
13From this table after the macro run I should have basically only the last row since it dosen't have duplicate. The first 4 rows are duplicates and should be deleted.
14
Sheet1



I'm going to ignore what you've written in column B and base the solution on the yellow highlighted cells.
 
Upvote 0
OK, with this code:

VBA Code:
Option Explicit
Sub Shwapx_V2()
    Dim ws As Worksheet
    Dim LRow As Long, LCol As Long
    Set ws = Worksheets("Sheet1")
    LRow = ws.Cells.Find("*", , xlFormulas, , 1, 2).Row
    LCol = ws.Cells.Find("*", , xlFormulas, , 2, 2).Column + 1
    Application.ScreenUpdating = False
  
    With ws.Range(ws.Cells(6, LCol), ws.Cells(LRow, LCol))
        .Value = ws.Evaluate(Replace("C6:C#&"" | ""&F6:F#&"" | ""&G6:G#&"" | ""&H6:H#&"" | ""&I6:I#", "#", LRow))
    End With
  
    Dim ArrIn, ArrOut, i As Long
    ArrIn = ws.Range(ws.Cells(5, LCol), ws.Cells(LRow + 1, LCol))
    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
    ws.Cells(5, LCol).Resize(UBound(ArrOut, 1)).Value = ArrOut
  
    With ws.Range(Cells(5, 1), Cells(LRow, LCol))
        .AutoFilter LCol, 1
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub

This
Shwapx.xlsm
ABCDEFGHIJKL
1Before
2
3Some info about the date
4Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11
5
620210801Addtesttest20000Test23Test44Test55Test66510
720210801Deletetesttest110000Test23Test44Test55Test661020
820210802AddTest23asd5Test11Test11Test11Test1122
920210801DeleteTest23asd5Test11Test11Test11Test1133
1020210803DeleteTest1asdasdTest11Test12Test13Test1112
11
Sheet1


Becomes this
Shwapx.xlsm
ABCDEFGHIJKL
1After
2
3Some info about the date
4Header1Header2Header3Header4Header5Header6Header7Header8Header9Header10Header11
5
620210803DeleteTest1asdasdTest11Test12Test13Test1112
7
Sheet1


Is that what you were looking for?
 
Upvote 0
Solution
Thank you so much I will take a look later at it since I'm busy at the moment. But as far as I can see from screenshots looks amazing!
 
Upvote 0
Thank you so much I will take a look later at it since I'm busy at the moment. But as far as I can see from screenshots looks amazing!
Happy to help, and thanks for the feedback :)
 
Upvote 0
Thank you it's working as expected. It's amazing. Just one last question is there a way to add the name of the excel file in the VBA so maybe I can export it and run it outside of excel? Export, the macro I mean and just run it and macro automatically open excel and does the magic :). Since I will need to adjust this to 400+ excel files I can try to include name of the file. And in my case, name of the file is the same as name of the sheet Example: name is "Dog" and sheet name will be named "Dog" as well. And at the end maybe I can try combining all macros in one big file and run it over those 400+ files.
 
Upvote 0
Thank you it's working as expected. It's amazing. Just one last question is there a way to add the name of the excel file in the VBA so maybe I can export it and run it outside of excel? Export, the macro I mean and just run it and macro automatically open excel and does the magic :). Since I will need to adjust this to 400+ excel files I can try to include name of the file. And in my case, name of the file is the same as name of the sheet Example: name is "Dog" and sheet name will be named "Dog" as well. And at the end maybe I can try combining all macros in one big file and run it over those 400+ files.
It should be relatively straightforward, especially if all those 400+ files are in the same folder (loop through each file in folder) however, what you're asking is considerably more code and not related to your original post - therefore you should start a new thread. When you do, explain exactly what each file looks like (I'm assuming they're the same layout), what you want to do (include the code from this thread) and provide the name of the folder etc. You can't supply too much information ;) FYI looping through files in folders isn't my area of interest, but there's plenty of other volunteers on this forum who'd will be willing to help, I'm sure :)
 
Upvote 0
@kevin9999 Thanks. I didn't express myself correctly sorry about that. I wanted to ask if there is an option to include in that macro the name of the excel file and the sheet in which macro to operate. So I can export the macro and run it out of excel.

Let's say I have a folder that has 3 excel files and that macro. When I run the macro to run it over the correct excel file and sheet. This will save the time of opening the excel and running the macro manually. Thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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