VBA to delete specific duplicate in column A based on column C

sunwave

New Member
Joined
Feb 28, 2024
Messages
9
Office Version
  1. 365
Platform
  1. Windows
I have two columns A and C. The column A has names, and column C has values 'Current' and 'Expired'.

There are duplicate names in column A and I want to remove the specific duplicate (entire row) that have value ‘Expired’ in column C.

Because the file is very large the formatting and filtering doesn’t work.

I want to run VBA to do that. What would be the code to achieve this?
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 
Upvote 0
I have two columns A and C. The column A has names, and column C has values 'Current' and 'Expired'.

There are duplicate names in column A and I want to remove the specific duplicate (entire row) that have value ‘Expired’ in column C.

Because the file is very large the formatting and filtering doesn’t work.

I want to run VBA to do that. What would be the code to achieve this?
 

Attachments

  • Capture.PNG
    Capture.PNG
    24.9 KB · Views: 8
Upvote 0
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary).
 

Attachments

  • Capture.PNG
    Capture.PNG
    24.9 KB · Views: 8
Upvote 0
From the screenshot posted, I want to delete the duplicate "Lauren" (column A) that is "expired" (column C) .
I want to delete the entire row instead of just the cell.
Thanks
 
Upvote 0
Try:
VBA Code:
Sub DeleteDups()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, lRow As Long, dic As Object
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("A2:A" & lRow).Resize(, 3).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = UBound(v) To LBound(v) Step -1
        If Not dic.exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
            If WorksheetFunction.CountIf(Range("A:A"), v(i, 1)) > 1 And v(i, 3) = "Expired" Then
                Rows(i + 1).Delete
            End If
        Else
            If WorksheetFunction.CountIf(Range("A:A"), v(i, 1)) > 1 And v(i, 3) = "Expired" Then
                Rows(i + 1).Delete
            End If
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution
Try:
VBA Code:
Sub DeleteDups()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, lRow As Long, dic As Object
    lRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    v = Range("A2:A" & lRow).Resize(, 3).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = UBound(v) To LBound(v) Step -1
        If Not dic.exists(v(i, 1)) Then
            dic.Add v(i, 1), Nothing
            If WorksheetFunction.CountIf(Range("A:A"), v(i, 1)) > 1 And v(i, 3) = "Expired" Then
                Rows(i + 1).Delete
            End If
        Else
            If WorksheetFunction.CountIf(Range("A:A"), v(i, 1)) > 1 And v(i, 3) = "Expired" Then
                Rows(i + 1).Delete
            End If
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
This worked liked magic! Thank you!!!
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,595
Members
452,657
Latest member
giadungthienduyen

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