VBA to delete duplicate rows that have specific text on another column

Status
Not open for further replies.

slora00

New Member
Joined
Sep 8, 2022
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Hello community, I hope everything is good.

I need help with the following situation. I have a file with duplicate values on column A, and a text "Keep" on column AC. I need to delete rows of duplicate values in column A that DO NOT have that "Keep" on column AC. I don't want to delete both duplicates and those rows that have "Keep", but those rows that meet both conditions.

Thank you in advance and I'll be waiting for an answer.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi slora00,

Try this (initially on a copy of your data as the results cannot be undone if they're not as expected):

VBA Code:
Option Explicit
Sub Macro1()

    Dim i As Long, j As Long, k As Long
    Dim cln As New Collection
    Dim xlnCalcMethod As XlCalculation
    Dim wsSrc As Worksheet
            
    With Application
        xlnCalcMethod = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    Set wsSrc = ThisWorkbook.Sheets("Sheet1") 'Sheet name containing the data. Change to suit if necessary.
    j = 2 'Starting row number. Change to suit if necessaty.
    k = wsSrc.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For i = k To j Step -1
        If wsSrc.Range("AC" & i) <> "Keep" Then
            On Error Resume Next
                cln.Add CStr(wsSrc.Range("A" & i)), wsSrc.Range("A" & i)
                If Err.Number <> 0 Then
                    wsSrc.Rows(i).Delete
                    Err.Clear
                End If
            On Error GoTo 0
        End If
    Next i
    
    With Application
        .Calculation = xlnCalcMethod
        .ScreenUpdating = True
    End With

End Sub

Regards,

Robert
 
Upvote 0
Duplicate to: VBA to delete duplicate and blank rows

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In relation to your question here, I have closed this thread so please continue in the linked thread.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,599
Messages
6,120,453
Members
448,967
Latest member
grijken

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