VBA: Match with ID in sheet 1 and delete rows with matching ID in other sheets

sr1111

New Member
Joined
Sep 2, 2022
Messages
46
Office Version
  1. 2013
  2. 2011
  3. 2010
  4. 2007
Platform
  1. Windows
VBA: Match with ID in sheet 1 and delete rows with matching ID in other sheets

Example

SheetX

idcustomer nameitemerror code
aaa_1@#$Gl
cd.1@#$Gl2
efg1@#3$Gl
1@#5$Gl

Sheet A input

idcustomer nameerror code1error code2
1@#5$Gl any name2any error
1@#5$Glany name1any error
1@#5$Gl4444customer name1error details1details2
pkkrh_&*$2detail2details2x2

Sheet A output

customer nameerror code1error code2
1@#5$Gl4444customer name1error details1details2
pkkrh_&*$2detail2details2x2
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this:

VBA Code:
Sub DeleteMatch()
  Dim shX As Worksheet, sh As Worksheet
  Dim dic As Object
  Dim c As Range
  Dim lr As Long
  
  Application.ScreenUpdating = False
  
  Set shX = Sheets("SheetX")
  Set dic = CreateObject("Scripting.Dictionary")
  
  For Each c In shX.Range("A2", shX.Range("A" & Rows.Count).End(3))
    If c.Value <> "" Then dic(c.Value) = Empty
  Next
  
  For Each sh In Sheets
    If sh.Name <> shX.Name Then
      lr = sh.Range("A" & Rows.Count).End(3).Row
      sh.Range("A1:A" & lr).AutoFilter 1, dic.keys, xlFilterValues
      sh.AutoFilter.Range.Offset(1).EntireRow.Delete
      sh.ShowAllData
    End If
  Next
  
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub DeleteMatch()
  Dim shX As Worksheet, sh As Worksheet
  Dim dic As Object
  Dim c As Range
  Dim lr As Long
 
  Application.ScreenUpdating = False
 
  Set shX = Sheets("SheetX")
  Set dic = CreateObject("Scripting.Dictionary")
 
  For Each c In shX.Range("A2", shX.Range("A" & Rows.Count).End(3))
    If c.Value <> "" Then dic(c.Value) = Empty
  Next
 
  For Each sh In Sheets
    If sh.Name <> shX.Name Then
      lr = sh.Range("A" & Rows.Count).End(3).Row
      sh.Range("A1:A" & lr).AutoFilter 1, dic.keys, xlFilterValues
      sh.AutoFilter.Range.Offset(1).EntireRow.Delete
      sh.ShowAllData
    End If
  Next
 
  Application.ScreenUpdating = True
End Sub

Thank you.
How to Match with ID in sheet 1 and delete rows with non-matching ID in other sheets"
 
Upvote 0
Here another approach. Two options given in the code, delete matching or non matching, depending on the formula which you are using.

VBA Code:
Sub jec()
 Dim sh, r
 For Each sh In Sheets
    If sh.Name <> Sheets("SheetX").Name Then
      With sh.Cells(1).CurrentRegion
         Set r = .Offset(, .Columns.Count + 1).Range("a1:a2")
'         r(2).Formula = "=isnumber(match(A2,SheetX!A:A,0))"      'delete matching
         r(2).Formula = "=iserror(match(A2,SheetX!A:A,0))"        'delete non matching
         .AdvancedFilter 1, r
         .Offset(1).EntireRow.Delete
         sh.ShowAllData
         r.ClearContents
      End With
    End If
 Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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