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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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
id5
id1
id7
id11
id15
id19
idsalesprofits
id25150361
id35295377this macro found all matching id numbers and deleted the rows
id45440393
id55585409if your list is on a different sheet, or in a different workbook
id65730425
id86020457just adjust the cell references
id96165473
id106310489
id126600521
id136745537For j = 2 To Cells(1, 14) + 1
id146890553 tryid = Cells(j, 13)
id167180585 For k = 45 To 11 Step -1
id177325601 If Cells(k, 1) <> tryid Then GoTo 100
id187470617 Rows(k).Delete
id207760649100 Next k
id67905665 Next j
id88195697200 End Sub
id98340713
id108485729
id128775761
id138920777
id149065793
id169355825
id179500841
id189645857
id209935889

<colgroup><col width="64" span="15" style="width:48pt"> </colgroup><tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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