delete duplicate rows leaving only the row with a certain value in a column

bobby13

Board Regular
Joined
Aug 14, 2008
Messages
78
what i am trying to do is to delete duplicate rows, but not just in the ordinary fashion. I want to search for duplicates based on cell contents in column A and C and then delete whichever duplicate contains the phrase "discontinue" in column E

So, if the contents in column A and C match for two rows then I want the macro to look to column E and delete whichever duplicate contains the word "discontinue"

I searched the forums and only found regular delete duplicates macros and nothing that will do just this.

Thanks for any help!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try:

Code:
Sub Test1()

    Dim CellA As String, CellC As String, CellE As String

    Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    
    Range("A2:A" & Lastrow).Select
    For Each cell In Selection
        CellA = cell.Value
        CellC = cell.Offset(0, 2).Value
        CellE = cell.Offset(0, 4).Value
        If CellA = cell.Offset(-1, 0).Value Then
            If CellC = cell.Offset(-1, 2).Value Then
                If CellE = "Discontinue" Then
                    cell.EntireRow.Delete
                End If
            End If
        End If
        CellA = ""
        CellC = ""
        CellE = ""
    Next
    
End Sub

HTH,

Jay
 
Upvote 0
Hi, This clears the cell !!, I imagined if you delete the row this also includes Data in "A" or "C" that you might want !!
Code:
Dim Rng1 As Range, Rng2 As Range, Dn1 As Range, Dn2 As Range
Set Rng1 = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
Set Rng2 = Range(Range("C1"), Range("C" & Rows.Count).End(xlUp))
  
  For Each Dn1 In Rng1
     For Each Dn2 In Rng2
            If Dn1 = Dn2 Then
                If Dn1.Offset(, 4) = "discontinue" Then
                    Dn1.ClearContents
                End If

                If Dn2.Offset(, 2) = "discontinue" Then
                    Dn2.ClearContents
                End If
            End If
    Next Dn2
Next Dn1
Mick
 
Upvote 0
thanks guys, they are both great. i'm getting better at vba but i'm still not too good; its always obvious once you see the correct vba.


thanks!!
 
Upvote 0

Forum statistics

Threads
1,213,520
Messages
6,114,101
Members
448,548
Latest member
harryls

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