Delete Row IF condition is met, IF NOT proceed to next condition etc

nremf

New Member
Joined
May 8, 2016
Messages
2
Hi everyone,

I've been googling my heart out but still haven't found the VBA code / solution I am after.
Let's say I've got 2 spreadsheets named spreadsheet1 and spreadsheet2.

In spreadsheet1 there are hundreds of rows with relevant data and I have already turned the autofilter on.
The unique identifier used in spreadsheet1 is a column called IDNUMBER which contains a mixtures of letters and numbers, e.g. JT123GH5845, JT123GH5846, JT123GH5847, JT123GH5848 ...

In spreadsheet2 I have a list of IDNUMBERS that need to be taken off the list in spreadsheet1. So, if you find any of the IDNUMBERs listed in spreadsheet2 delete the row that contains that IDNUMBERS than proceed to the next. IF you can't find an IDNUMBER listed in spreadsheet2 proceed to the next one.

Example:
spreadsheet1 contains the above IDNUMBERs
IDNUMBER
JT123GH5845 - Row 1
JT123GH5846 - Row 2
JT123GH5847 - Row 3
JT123GH5848 - Row 4
JT123GH5860 - Row 5

spreadsheet2 contains the following IDNUMBERs
IDNUMBER
JT123GH5834
JT123GH5845
JT123GH5846
JT123GH5849
JT123GH5850

Therefore, the VBA code should get rid of Row 1 and Row 2 in spreadsheet1

As I did not know how to do a match up between the 2 spreadsheets I started off with deleting the individual entry by selecting the ID number and then just deleting the content of the row rather than the row itself, but it takes too long to maintain, now that the spreadsheet2 has become quite long. Furthermore, it seemed that, once an ID number wasn't found it skipped straight to the end of the macro rather than skipping to the next IDnumber. I probably have set the On Error Resume Next in the wrong position...

Sub CleanseAsPerTakeOffList()
'
' CleanseAsPerTakeOffList Macro
'
On Error Resume Next
'
' Open Spread Sheet: spreadsheet1
Sheets("spreadsheet1").Select
'
' Delete row in which a specific ID number is found via Filtering ID-Column
ActiveSheet.ListObjects("Table5").Range.AutoFilter Field:=8, Criteria1:= _
"JT123GH5834"
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
' Clear filter of ID column to restart process for new ID number
ActiveSheet.ListObjects("Table5").Range.AutoFilter Field:=8
'
End Sub

So, best case scenario would be spreadsheet1 gets cleansed according to a list in spreadsheet2 so that instead of constantly updating the macro I can just update spreadsheet2 and the macro just keeps on going until it's finished with cross-checking against spreadsheet2.

THANK YOU SO MUCH FOR YOUR HELP. This one has been bugging me for too long and none of the threads I googled showed me how to solve this, but maybe I am looking in the wrong direction, as I can usually find the VBA code I am after...

Nic
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi and welcome to the forum. Try this...

Code:
[color=darkblue]Sub[/color] CleanseAsPerTakeOffList()
[color=green]'[/color]
[color=green]' CleanseAsPerTakeOffList Macro[/color]
[color=green]'[/color]
    
    [color=darkblue]Dim[/color] cell [color=darkblue]As[/color] Range, vMatch [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    [color=darkblue]Dim[/color] ws1 [color=darkblue]As[/color] Worksheet, ws2 [color=darkblue]As[/color] Worksheet
    
    [color=darkblue]Set[/color] ws1 = Sheets("Sheet1")  [color=green]'Data sheet[/color]
    [color=darkblue]Set[/color] ws2 = Sheets("Sheet2")  [color=green]'List of IDs to delete[/color]
    
    Application.ScreenUpdating = [color=darkblue]False[/color]
    [color=darkblue]For[/color] [color=darkblue]Each[/color] cell [color=darkblue]In[/color] ws2.Range("A1", ws2.Range("A" & Rows.Count).End(xlUp)) [color=green]'ID list in column A[/color]
        vMatch = Application.Match(cell.Value, ws1.Columns(8), 0)           [color=green]'Match IDs in column 8[/color]
        [color=darkblue]If[/color] IsNumeric(vMatch) [color=darkblue]Then[/color] ws1.Rows(vMatch).Delete
    [color=darkblue]Next[/color] cell
    Application.ScreenUpdating = [color=darkblue]True[/color]
    
    MsgBox "Delete rows complete."
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
[TABLE="width: 960"]
<colgroup><col width="64" span="15" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[TD="width: 64"]id[/TD]
[TD="width: 64, align: right"]5[/TD]
[TD="width: 64"][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]id1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]id7[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]id11[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]id15[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]id19[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id[/TD]
[TD]sales[/TD]
[TD]profits[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id2[/TD]
[TD="align: right"]5150[/TD]
[TD="align: right"]361[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id3[/TD]
[TD="align: right"]5295[/TD]
[TD="align: right"]377[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 6"]this macro found all matching id numbers and deleted the rows[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id4[/TD]
[TD="align: right"]5440[/TD]
[TD="align: right"]393[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id5[/TD]
[TD="align: right"]5585[/TD]
[TD="align: right"]409[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 6"]if your list is on a different sheet, or in a different workbook[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id6[/TD]
[TD="align: right"]5730[/TD]
[TD="align: right"]425[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id8[/TD]
[TD="align: right"]6020[/TD]
[TD="align: right"]457[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 3"]just adjust the cell references[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id9[/TD]
[TD="align: right"]6165[/TD]
[TD="align: right"]473[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id10[/TD]
[TD="align: right"]6310[/TD]
[TD="align: right"]489[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id12[/TD]
[TD="align: right"]6600[/TD]
[TD="align: right"]521[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id13[/TD]
[TD="align: right"]6745[/TD]
[TD="align: right"]537[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 3"]For j = 2 To Cells(1, 14) + 1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id14[/TD]
[TD="align: right"]6890[/TD]
[TD="align: right"]553[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"] tryid = Cells(j, 13)[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id16[/TD]
[TD="align: right"]7180[/TD]
[TD="align: right"]585[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 3"] For k = 45 To 11 Step -1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id17[/TD]
[TD="align: right"]7325[/TD]
[TD="align: right"]601[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 4"] If Cells(k, 1) <> tryid Then GoTo 100[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id18[/TD]
[TD="align: right"]7470[/TD]
[TD="align: right"]617[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"] Rows(k).Delete[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id20[/TD]
[TD="align: right"]7760[/TD]
[TD="align: right"]649[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"]100 Next k[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id6[/TD]
[TD="align: right"]7905[/TD]
[TD="align: right"]665[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] Next j[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id8[/TD]
[TD="align: right"]8195[/TD]
[TD="align: right"]697[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"]200 End Sub[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id9[/TD]
[TD="align: right"]8340[/TD]
[TD="align: right"]713[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id10[/TD]
[TD="align: right"]8485[/TD]
[TD="align: right"]729[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id12[/TD]
[TD="align: right"]8775[/TD]
[TD="align: right"]761[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id13[/TD]
[TD="align: right"]8920[/TD]
[TD="align: right"]777[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id14[/TD]
[TD="align: right"]9065[/TD]
[TD="align: right"]793[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id16[/TD]
[TD="align: right"]9355[/TD]
[TD="align: right"]825[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id17[/TD]
[TD="align: right"]9500[/TD]
[TD="align: right"]841[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id18[/TD]
[TD="align: right"]9645[/TD]
[TD="align: right"]857[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]id20[/TD]
[TD="align: right"]9935[/TD]
[TD="align: right"]889[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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