Delete row if value is found in another workbook

K1600

Board Regular
Joined
Oct 20, 2017
Messages
166
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.
 

Some videos you may like

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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
 

K1600

Board Regular
Joined
Oct 20, 2017
Messages
166
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?
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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
 

K1600

Board Regular
Joined
Oct 20, 2017
Messages
166

ADVERTISEMENT

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.
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
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"
 

K1600

Board Regular
Joined
Oct 20, 2017
Messages
166
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,331
Messages
5,624,063
Members
416,010
Latest member
NJT

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
Top