vba Delete Row(s) not matching table criteria Macro

NGB82KS

Board Regular
Joined
Nov 7, 2019
Messages
82
Office Version
  1. 2016
So i'll do my best to try and explain this.
I have a spreadsheet with 2 tabs (DataExport; FilterCriteria)
The DataExport tab is every computer i pulled from active directory (real long list) Column A is the PC name and Column B is the OU it resides in.
The FilterCriteria tab Column A is a list of the PC names that I would be looking for.
Note* Our PC naming conventions have gone through some changes, so i need the search to look for anything that begins, ends or contains the computer name in Column A of the FilterCriteria tab.

I need to be able to execute a macro that Deletes all rows from the DataExport tab that doesn't match the name filters in Column A/Name table of the FilterCriteria tab. Any help would be greatly appreciated.
 
Thank you for your support, it still error when I compare Result A2 sheet vs DLV030 AU column sheet

1681950992046.png

1681951027204.png
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Sorry, it works perfectly. Thank you so much.

Sub Delete_Row_1()
Dim arr As Variant, i As Long, j As Long, lr As Long
Dim a As Variant, b As Variant, r As Range
Dim sh1 As Worksheet, sh2 As Worksheet, exists As Boolean

Application.ScreenUpdating = False
'
Set sh1 = Sheets("DLV030")
Set sh2 = Sheets("Result")
If sh1.AutoFilterMode Then sh1.AutoFilterMode = False
lr = sh1.Range("AU" & Rows.Count).End(xlUp).Row
a = sh1.Range("AU2:AU" & lr).Value2

b = sh2.Range("A2:A" & sh2.Range("A" & Rows.Count).End(xlUp).Row + 1).Value
Set r = sh1.Range("A" & lr + 1)
For i = 1 To UBound(a)
exists = False
For j = 1 To UBound(b) - 1
If a(i, 1) Like "*" & b(j, 1) & "*" Then
exists = True
Exit For
End If
Next
If exists = False Then Set r = Union(r, sh1.Range("A" & i + 1))
Next
r.EntireRow.Delete
End Sub
 
Upvote 1

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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