VBA script to leave only unique rows

davidlatics

New Member
Joined
Jan 18, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi, I wonder if anyone can help me please – I’ve tried searching previous posts but I’m unable to find a solution to my query.

Spreadsheet 1 consists of hundreds of rows of data - a sample is below. Spreadsheet 2 includes exactly the same rows as Spreadsheet 1, plus a handful of new entries. However, these new rows could be located anywhere within the sheet.

I would like to find a way of deleting all the duplicate rows on Spreadsheet 2 (when compared with Spreadsheet 1) to leave only the new rows. I have to repeat this process each day (i.e. comparing that day’s data to the previous day) so any help would be massively appreciated. I am using Office 365.

NA 0115.xlsx
ABCDEFGHI
1Op. AlOriginDepAptNameDepCtryNameArrAptNameArrCityNameArrCtryNameDestination
22DBOSBoston Edward L Logan Intl AptUSABelo Horizonte Tancredo Neves Int AptBelo HorizonteBrazilCNF
32DYYZToronto Lester B Pearson IntlCanadaGeorgetown (GY) 00Georgetown (GY) 00GuyanaGEO
44BCNMCarlsbadUSAEl Paso International AptEl PasoUSAELP
54BELPEl Paso International AptUSACarlsbadCarlsbadUSACNM
6AAAUSAustin-Bergstrom International AptUSABoston Edward L Logan Intl AptBostonUSABOS
7AAAUSAustin-Bergstrom International AptUSAOrlando International AptOrlandoUSAMCO
8AAAUSAustin-Bergstrom International AptUSASan Jose Norman Y. Mineta IntlSan Jose (US)USASJC
9AABOSBoston Edward L Logan Intl AptUSAAustin-Bergstrom International AptAustin (US) TXUSAAUS
10AABOSBoston Edward L Logan Intl AptUSAFort Lauderdale/Hollywood Intl AptFort LauderdaleUSAFLL
11AABOSBoston Edward L Logan Intl AptUSAHilton Head IslandHilton Head IslandUSAHHH
12ACYOWOttawa McDonald - Cartier Intl AptCanadaCayo CocoCayo CocoCubaCCC
13ACYULMontreal Pierre Elliott Trudeau Int AptCanadaToulouseToulouseFranceTLS
14ACYYZToronto Lester B Pearson IntlCanadaBrussels AirportBrusselsBelgiumBRU
15ASAUSAustin-Bergstrom International AptUSALos Angeles International AptLos Angeles (US)USALAX
16ASBOIBoiseUSAEverettEverettUSAPAE
17AZSFOSan FranciscoUSARome Fiumicino AptRome (IT)ItalyFCO
18B6ATLAtlanta Hartsfield-jackson Intl AptAtlantaUSANewark Liberty International AptNew YorkUSAEWR
Sheet1
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
in your table, is the arraptname the unique member ? because as I can see there are duplicates in all other column except the arraptname column. can you please enlighten
 
Upvote 0
If you want a VBA code, some code can be done - some people on here amaze me with the brevity of how they write code (I've been meat-balling excel code for 25 years.)

But you might use column J on both spreadsheets as =concatenate(a2,b2,c2,...i2)

Then in column k of the new sheet =ifna(vlookup(j2,sheet1!$j$2:$j$99,1,false),"NEW DATA")

Delete anything that isn't "NEW DATA". Of course, if I had to do it every day, I'd write some code for it. I'd want to load a variable array with the concatenated values from sheet1 then go to sheet 2 and compare each concatenated row to the array from sheet one and if it was found, delete the row. But I'll be interested in how some of the real pros would do it with less code and more speed.
 
Upvote 0
in your table, is the arraptname the unique member ? because as I can see there are duplicates in all other column except the arraptname column. can you please enlighten
Hi, no unfortunately there are no unique members. For example, here is today's spreadsheet with the new row highlighted in red. The rest of the sheet remains the same, apart from the new row.


Op. AlOriginDepAptNameDepCtryNameArrAptNameArrCityNameArrCtryNameDestination
2DBOSBoston Edward L Logan Intl AptUSABelo Horizonte Tancredo Neves Int AptBelo HorizonteBrazilCNF
2DYYZToronto Lester B Pearson IntlCanadaGeorgetown (GY) 00Georgetown (GY) 00GuyanaGEO
4BCNMCarlsbadUSAEl Paso International AptEl PasoUSAELP
4BELPEl Paso International AptUSACarlsbadCarlsbadUSACNM
AAAUSAustin-Bergstrom International AptUSABoston Edward L Logan Intl AptBostonUSABOS
AAAUSAustin-Bergstrom International AptUSAOrlando International AptOrlandoUSAMCO
AAAUSAustin-Bergstrom International AptUSASan Jose Norman Y. Mineta IntlSan Jose (US)USASJC
AABOSBoston Edward L Logan Intl AptUSAAustin-Bergstrom International AptAustin (US) TXUSAAUS
AABOSBoston Edward L Logan Intl AptUSAFort Lauderdale/Hollywood Intl AptFort LauderdaleUSAFLL
AABOSBoston Edward L Logan Intl AptUSAHilton Head IslandHilton Head IslandUSAHHH
AAEWRNewark Liberty International AptUSABelo Horizonte Tancredo Neves Int AptBelo HorizonteBrazilCNF
ACYOWOttawa McDonald - Cartier Intl AptCanadaCayo CocoCayo CocoCubaCCC
ACYULMontreal Pierre Elliott Trudeau Int AptCanadaToulouseToulouseFranceTLS
ACYYZToronto Lester B Pearson IntlCanadaBrussels AirportBrusselsBelgiumBRU
ASAUSAustin-Bergstrom International AptUSALos Angeles International AptLos Angeles (US)USALAX
ASBOIBoiseUSAEverettEverettUSAPAE
AZSFOSan FranciscoUSARome Fiumicino AptRome (IT)ItalyFCO
B6ATLAtlanta Hartsfield-jackson Intl AptAtlantaUSANewark Liberty International AptNew YorkUSAEWR
 
Upvote 0
Is your end goal to know which one is new, and the new list of all unique rows will be back in Sheet 1 again (preparing for next day's data)?

If yes, you can try this. This assumes your data starts from A1:
VBA Code:
Sub me1158814_removedupes()
    Dim ws1 As Worksheet, ws2 As Worksheet, r As Long, i As Long, a
    Set ws1 = Sheets("Sheet1") 'edit accordingly
    Set ws2 = Sheets("Sheet2") 'edit accordingly
   
    r = ws1.Cells(1, 1).CurrentRegion.Rows.Count + 1
    With ws2.Cells(1, 1).CurrentRegion
        ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).Value = .Offset(1).Resize(.Rows.Count - 1, .Columns.Count).Value
        ReDim a(0 To .Columns.Count - 1)
        For i = 0 To UBound(a)
            a(i) = i + 1
        Next
    End With
    ws1.Cells(1, 1).CurrentRegion.RemoveDuplicates Columns:=(a), Header:=xlYes
    With ws1.Cells(1, 1).CurrentRegion
        .Range(.Cells(r, 1), .Cells(.Rows.Count, .Columns.Count)).Font.Color = vbRed
    End With
End Sub

It moves whatever you have in Sheet2 into Sheet1, and uses Excel's built in function to remove duplicates. New unique rows that was from Sheet2 will now be in red font in Sheet1 (at the bottom of the list).
 
Upvote 0
Is your end goal to know which one is new, and the new list of all unique rows will be back in Sheet 1 again (preparing for next day's data)?

If yes, you can try this. This assumes your data starts from A1:
VBA Code:
Sub me1158814_removedupes()
    Dim ws1 As Worksheet, ws2 As Worksheet, r As Long, i As Long, a
    Set ws1 = Sheets("Sheet1") 'edit accordingly
    Set ws2 = Sheets("Sheet2") 'edit accordingly
  
    r = ws1.Cells(1, 1).CurrentRegion.Rows.Count + 1
    With ws2.Cells(1, 1).CurrentRegion
        ws1.Cells(ws1.Rows.Count, 1).End(xlUp).Offset(1).Resize(.Rows.Count - 1, .Columns.Count).Value = .Offset(1).Resize(.Rows.Count - 1, .Columns.Count).Value
        ReDim a(0 To .Columns.Count - 1)
        For i = 0 To UBound(a)
            a(i) = i + 1
        Next
    End With
    ws1.Cells(1, 1).CurrentRegion.RemoveDuplicates Columns:=(a), Header:=xlYes
    With ws1.Cells(1, 1).CurrentRegion
        .Range(.Cells(r, 1), .Cells(.Rows.Count, .Columns.Count)).Font.Color = vbRed
    End With
End Sub

It moves whatever you have in Sheet2 into Sheet1, and uses Excel's built in function to remove duplicates. New unique rows that was from Sheet2 will now be in red font in Sheet1 (at the bottom of the list).
Thanks so much - this is exactly what I was looking for.
 
Upvote 0

Forum statistics

Threads
1,214,390
Messages
6,119,235
Members
448,879
Latest member
VanGirl

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