Delete rows based on data in other rows

ChrisCione

Board Regular
Joined
Aug 27, 2008
Messages
92
Office Version
  1. 365
Platform
  1. Windows
A system we use generates a report which looks like the sample data shown below. Note that names are repeated at least once (and often more than once). The "key" column is the confirmation indicator of Y or N (yes or no). The goal is to eliminate all duplicates under the following conditions: 1) If a person has confirmed (noted by a "Y") that information would only appear once on the report. If a person has NOT confirmed (noted by an "N"), that information would also only appear once. All we need to know is if a person confirmed or not. I've read other threads in the forum, and I'm assuming this will require code, which I have no experience in writing.

Sample data:

First NameLast NameConfirmation
MickeyMouseY
MickeyMouseN
MinnieMouseN
MinnieMouseN
GoofyDogN
GoofyDogN
GoofyDogN
GoofyDogN
DaffyDuckY
DaffyDuckY
DaffyDuckN
DaffyDuckN

The desired result:

First NameLast NameConfirmation
MickeyMouseY
MinnieMouseN
GoofyDogN
DaffyDuckY
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Here's how I would accomplish this with code. Note this code assumes that your data is in columns A-C and that there is no additional data below your table:
VBA Code:
Sub Dedupe()

Dim lrow As Long
Dim i As Long
Dim j As Long
Dim Name As String
Dim Con As String

lrow = Cells(Rows.Count, 1).End(xlUp).row

For i = 2 To lrow
    Name = Cells(i, 1) & Cells(i, 2)
    Con = Cells(i, 3)
    
    For j = 2 To lrow
        If j = i Then GoTo nextj
        If Con = "Y" And (Cells(j, 1) & Cells(j, 2) = Name) Then
            Rows(j).Delete
            j = j - 1
            GoTo nextj
        End If
        
        If Con = "N" And (Cells(j, 1) & Cells(j, 2) = Name And Cells(j, 3) = "N" And i < j) Then
            Rows(j).Delete
            j = j - 1
            GoTo nextj
        End If
    
nextj:
    Next j

Next i

End Sub
 
Upvote 0
an alternative with Power Query
Load your table to PQ and run this Mcode
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each ([Confirmation] = "Y")),
    #"Removed Duplicates" = Table.Distinct(#"Filtered Rows")
in
    #"Removed Duplicates"


Next, Join the above query back onto the original data
Power Query:
let
    Source = Table.NestedJoin(#"Table1 (2)", {"First Name", "Last Name"}, Table1, {"First Name", "Last Name"}, "Table1", JoinKind.FullOuter),
    #"Expanded Table1" = Table.ExpandTableColumn(Source, "Table1", {"Confirmation"}, {"Confirmation.1"}),
    #"Removed Duplicates" = Table.Distinct(#"Expanded Table1", {"First Name", "Last Name", "Confirmation.1"})
in
    #"Removed Duplicates"
 
Upvote 0
Solution
Here's how I would accomplish this with code. Note this code assumes that your data is in columns A-C and that there is no additional data below your table:
VBA Code:
Sub Dedupe()

Dim lrow As Long
Dim i As Long
Dim j As Long
Dim Name As String
Dim Con As String

lrow = Cells(Rows.Count, 1).End(xlUp).row

For i = 2 To lrow
    Name = Cells(i, 1) & Cells(i, 2)
    Con = Cells(i, 3)
   
    For j = 2 To lrow
        If j = i Then GoTo nextj
        If Con = "Y" And (Cells(j, 1) & Cells(j, 2) = Name) Then
            Rows(j).Delete
            j = j - 1
            GoTo nextj
        End If
       
        If Con = "N" And (Cells(j, 1) & Cells(j, 2) = Name And Cells(j, 3) = "N" And i < j) Then
            Rows(j).Delete
            j = j - 1
            GoTo nextj
        End If
   
nextj:
    Next j

Next i

End Sub
Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,148
Messages
6,123,309
Members
449,095
Latest member
Chestertim

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