Delete Lines that possess one of the criteria in this list

B5rocksass

Board Regular
Joined
Jan 10, 2017
Messages
56
Office Version
  1. 2016
Platform
  1. Windows
I have a report that i run for departmental metrics but the report has some lines that should not be factoring in. I would like to remove them with a macro, but i'm unsure if I'll get consistent results if I record it as 'filtering to delete for each criteria' in the list. There has to be a better way. Please note that all the criteria will only ever be found in one particular column. I mocked up my example and added a small list to delete the lines with that criteria. I'm hoping someone can help me design the macro. I don't know much about VBA unfortunately. Please advise.

Test.xlsm
ABCDEF
1PO NumberPO DateVendor NameVendor #Item NumberItem Description
22229871/22/2019NORTH AMERICA16034243005.905DIAMETER X18.504WI
322913811/8/2019VISION TECHNOLOGIES1568515000-UMLK-1Item-1
42311782/21/2020VISION TECHNOLOGIES1568515000-UMLK-5Item-5
523572610/8/2020TRUST CORPORATION162710RS-SAAS-MOORCLOUD LICENSE
623572610/8/2020TRUST CORPORATION162710RS-SAAS-WBCLOUD LICENSE
72375911/25/2021SET-RITE OF BONNEVILLE121671D0390013ZItem-1
82380662/12/2021SET-RITE OF BONNEVILLE161963D0390013ZItem-3
92390233/29/2021NORTH AMERICA,INC.96808189.429.10varnish
102390823/31/2021ELECTRONICS FOR ALL15581370970519CONNECTOR 90 DEG 4 PIN SCR
112395754/21/2021CRANE-IT161597CRANE INSPECTANNUAL INSPECTION
122395754/21/2021CRANE-IT161597TRUCK CHARGETRUCK CHARGE
132397194/27/2021ELECTRONICS FOR ALL15581371093991PANEL MOUTN RECEPTABLE KIT FEM
142397374/28/2021ELECTRONICS FOR ALL15581370007012LIGHT BLOCK ASSEMBLY 110.120VA
152398575/3/2021ACTEGA NORTH AMERICA,INC.96808110003698varnish
162398855/4/2021ELECTRONICS FOR ALL15581370008931ELECTRIC-LIGHT MODLE
172399205/5/2021ELECTRONICS FOR ALL15581370176810CONTACT REMOVAL KIT
182399225/5/2021WIPERS FOR YOU15618150LBSWHITE SHEETING
192399335/5/2021MARO INDUSTRIES, INC.163235P4349FIRE EXTINGUISHER (DO NO BLOCK)
202399335/5/2021MARO INDUSTRIES, INC.163235C2226-CECUSTOM FLOOR SIGN
212400045/7/2021AKLODA INC91614054615015X15X6 CORRUGATE
22
23Vendor# to Remove
24160342
25156851
26162710
27121671
28161963
Test
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Expand the col D range to cover all your data. Remove or move the target Vendor# List from col D.

VBA Code:
Sub RemoveVendors()
Dim R As Range, Remove As Variant
Set R = Range("D2:D36") 'change range to suit
Remove = Array(160342, 156851, 162710, 121671, 161963)
For i = LBound(Remove) To UBound(Remove)
    R.Replace Remove(i), replacement:="#N/A"
Next i
On Error Resume Next
R.SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
On Error GoTo 0
End Sub
 
Upvote 0
Hi Joe! Thank you for looking at this and offering your resolution.
I erased the target vendor list as you explained...but I honestly do not know what to do with your code, since I have never learned VBA. How do i create a macro with copy and paste code?
 
Upvote 0
Hi Joe! Thank you for looking at this and offering your resolution.
I erased the target vendor list as you explained...but I honestly do not know what to do with your code, since I have never learned VBA. How do i create a macro with copy and paste code?
To install standard module code:
1. With your workbook active press Alt and F11 keys. This will open the VBE window.
2. In the project tree on the left of the VBE window, find your project and click on it.
3. On the VBE menu: Insert>Module
4. Copy the code from your browser window and paste it into the white space in the VBE window.
5. Close the VBE window and Save the workbook. If you are using Excel 2007 or a later version do a SaveAs and save it as a macro-enabled workbook (.xlsm file extension).
6. Press Alt+F8 keys to run the code
7. Make sure you have enabled macros whenever you open the file or the code will not run.
 
Upvote 0
Okay, almost there...it threw an error on the range. How to i update the code to look for all rows in the entire column D? I'm asking since every report has either more or less rows.
 
Upvote 0
Okay, almost there...it threw an error on the range. How to i update the code to look for all rows in the entire column D? I'm asking since every report has either more or less rows.
Replace the existing code with this:
VBA Code:
Sub RemoveVendors()
Dim R As Range, Remove As Variant
Set R = Range("D2:D" & Cells(Rows.Count, "D").End(xlUp).Row)
Remove = Array(160342, 156851, 162710, 121671, 161963)
For i = LBound(Remove) To UBound(Remove)
    R.Replace Remove(i), replacement:="#N/A"
Next i
On Error Resume Next
R.SpecialCells(xlCellTypeConstants, xlErrors).EntireRow.Delete
On Error GoTo 0
End Sub
 
Upvote 0
Solution
That worked great!
Now that the macro is made, how do i make it available to any report as opposed to just this one?
 
Upvote 0
That worked great!
Now that the macro is made, how do i make it available to any report as opposed to just this one?
You can run it on any data set you have after you make any needed changes to the vendor ID column and/or the vendor Id's you wish to remove.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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