Macro to delete row based on column A

robrobet

New Member
Joined
Jun 3, 2013
Messages
27
Office Version
  1. 365
  2. 2019
HI,

I have a sheet with thousands entry and I want a macro that looks column A (which is ID number) if macro find a match than delete all of those rows.
example Sheet 1,
Column A
8888 ABC
8888 ABC
7777 ABC
8888 ABC

When macro finish I should only see the row with 7777ABC.
Thanks in Advance.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
HI,

I have a sheet with thousands entry and I want a macro that looks column A (which is ID number) if macro find a match than delete all of those rows.
example Sheet 1,
Column A
8888 ABC
8888 ABC
7777 ABC
8888 ABC

When macro finish I should only see the row with 7777ABC.
Thanks in Advance.
Try this
Is this helpful to you or not


VBA Code:
Sub Duplicate()

    Dim ws As Worksheet
    Dim rCheck As Range
    Dim rDel As Range

    Set ws = ActiveWorkbook.ActiveSheet

    For Each rCheck In ws.Range("A1", ws.Cells(ws.Rows.Count, "A").End(xlUp)).Cells
        If WorksheetFunction.CountIf(ws.Columns("A"), rCheck.Value) > 1 Then
            If Not rDel Is Nothing Then
                Set rDel = Union(rDel, rCheck)
            Else
                Set rDel = rCheck
            End If
        End If
    Next rCheck

    If Not rDel Is Nothing Then rDel.EntireRow.Delete

End Sub
 
Upvote 0
You could also try this with a copy of your workbook.
If it takes too long post back for a longer code but faster execution.

VBA Code:
Sub DeleteDupes()
  Application.ScreenUpdating = False
  With Range("A1", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate(Replace("if(countif(@,@)>1,""#N/A"",@)", "@", .Address))
    On Error Resume Next
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi, a VBA demonstration which should be fast enough :​
VBA Code:
Sub Demo1()
    Dim S&
        Application.ScreenUpdating = False
    With [A1].CurrentRegion.Columns
       .Item(.Count + 1).Formula = "=--(COUNTIF(" & .Item(1).Address & ",A1)>1)"
        S = Application.Sum(.Item(.Count + 1))
     If S Then
       .Resize(, .Count + 1).Sort .Item(.Count + 1), 1
       .Rows(.Rows.Count - S + 1 & ":" & .Rows.Count).Clear
     End If
       .Item(.Count + 1).Clear
    End With
        Application.ScreenUpdating = True
End Sub
 
Upvote 0
You're welcome. Glad we could help. Thanks for the follow-up.
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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