Delete row if value is found in another workbook

K1600

Board Regular
Joined
Oct 20, 2017
Messages
181
I have two workbooks, one containing my master data (Filename: VPRS) with the sheet called "Pro" and the other with a list of entries to check (Filename: ANP) with the data in a sheet called "Sheet_CLEAR".

I basically want to search column 'A' in the "Sheet_CLEAR" sheet for any value in column 'F' of my "Pro" sheet and if found, then delete the entire row in the "Sheet_CLEAR" sheet. The data in the "Pro" sheet is actually formatted as a table (Table8) and the column header is "VRM / ID" if it is possible to use that instead of the column reference.

I have tried a variety of options found online but none seem to quite work for me.

Any help would be gratefully received.
 

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. You might need to change the file extensions that I used.

VBA Code:
Sub t()
Dim sh1 As Worksheet, sh2 As Worksheet, i As Long, fn As Range
Set sh1 = Workbooks("VPRS.xlsm").Sheets("Pro") 'Edit file extension type
Set sh2 = Workbooks("ANP.xlsx").Sheets("Sheet_CLEAR")  'Edit file extension type
    For i = sh2.Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        Set fn = sh1.Columns("F").Find(sh2.Cells(i, 1).Value)
        If Not fn Is Nothing Then Rows(i).Delete
    Next
End Sub
 
Upvote 0
Try this. You might need to change the file extensions that I used.

VBA Code:
Sub t()
Dim sh1 As Worksheet, sh2 As Worksheet, i As Long, fn As Range
Set sh1 = Workbooks("VPRS.xlsm").Sheets("Pro") 'Edit file extension type
Set sh2 = Workbooks("ANP.xlsx").Sheets("Sheet_CLEAR")  'Edit file extension type
    For i = sh2.Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        Set fn = sh1.Columns("F").Find(sh2.Cells(i, 1).Value)
        If Not fn Is Nothing Then Rows(i).Delete
    Next
End Sub
Spot on, thank you.

I am going to get it to save the ANP workbook as a csv file once it's removed the necessary rows but is it possible to add a bit to the code that if the process found any entries and subsequently deleted the rows that a MsgBox could show to let me know that changes have been made to that workbook/csv file?
 
Upvote 0
Maybe this

VBA Code:
Sub t()
Dim sh1 As Worksheet, sh2 As Worksheet, i As Long, fn As Range, flg As Boolean
Set sh1 = Workbooks("VPRS.xlsm").Sheets("Pro") 'Edit file extension type
Set sh2 = Workbooks("ANP.xlsx").Sheets("Sheet_CLEAR")  'Edit file extension type
    For i = sh2.Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        Set fn = sh1.Columns("F").Find(sh2.Cells(i, 1).Value)
        If Not fn Is Nothing Then 
            Rows(i).Delete
            flg = True
        End If
    Next
If flg = True Then MsgBox  "Itens deleted from "Sheet_CLEAR", vbInformation, "ITEMS DELETED"
End Sub
 
Upvote 0
Maybe this

VBA Code:
Sub t()
Dim sh1 As Worksheet, sh2 As Worksheet, i As Long, fn As Range, flg As Boolean
Set sh1 = Workbooks("VPRS.xlsm").Sheets("Pro") 'Edit file extension type
Set sh2 = Workbooks("ANP.xlsx").Sheets("Sheet_CLEAR")  'Edit file extension type
    For i = sh2.Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        Set fn = sh1.Columns("F").Find(sh2.Cells(i, 1).Value)
        If Not fn Is Nothing Then
            Rows(i).Delete
            flg = True
        End If
    Next
If flg = True Then MsgBox  "Itens deleted from "Sheet_CLEAR", vbInformation, "ITEMS DELETED"
End Sub
That's perfect. Thank you so much.
 
Upvote 0
You probably figured it out but for the record, here is a corrected line of code for the message box.

VBA Code:
If flg = True Then MsgBox "Itens deleted from 'Sheet_CLEAR'", vbInformation, "ITEMS DELETED"
 
Upvote 0
You probably figured it out but for the record, here is a corrected line of code for the message box.

VBA Code:
If flg = True Then MsgBox "Itens deleted from 'Sheet_CLEAR'", vbInformation, "ITEMS DELETED"
I did indeed but thanks for clarifying it.
 
Upvote 0

Forum statistics

Threads
1,213,567
Messages
6,114,344
Members
448,570
Latest member
rik81h

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