How can I compare 2 columns on seperate sheets to delete records not found?

mecerrato

Board Regular
Joined
Oct 5, 2015
Messages
174
Office Version
  1. 365
Platform
  1. Windows
I have 2 sheets, I want to search the loan number in column A of my "Main" sheet and compare it to Column C if my "data" worksheet. If it is not found I want a msgbox to ask if I should delete the row from the "Main" sheet. I can so the delete and the msgbox but I really don't know how to write the search code. I tried a few things from a google search but can't get it done.
2020-12-19 19_48_46-Book1 - Excel.png
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
It looks like your two tables might be formal Excel tables (ListObjects). If that is so, see if you can adapt this.
I have assumed the table on 'Main' is named "tblMain" and the table on 'Data' is named "tblData" (& that it is ListObject(1) on that sheet) so adjust the code to match that (& check the header values on the relevant columns)

VBA Code:
Dim cell As Range, rngData As Range, LoanFound As Range

With Sheets("Data")
  .ListObjects(1).AutoFilter.ShowAllData
  Set rngData = .Range("tblData[Loan '#]")
End With
For Each cell In Range("tblMain[Loan '#]")
  Set LoanFound = rngData.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
  If LoanFound Is Nothing Then
    'Put your code here for MsgBox/Delete etc
   
  End If
Next cell
 
Upvote 0
It looks like your two tables might be formal Excel tables (ListObjects). If that is so, see if you can adapt this.
I have assumed the table on 'Main' is named "tblMain" and the table on 'Data' is named "tblData" (& that it is ListObject(1) on that sheet) so adjust the code to match that (& check the header values on the relevant columns)

VBA Code:
Dim cell As Range, rngData As Range, LoanFound As Range

With Sheets("Data")
  .ListObjects(1).AutoFilter.ShowAllData
  Set rngData = .Range("tblData[Loan '#]")
End With
For Each cell In Range("tblMain[Loan '#]")
  Set LoanFound = rngData.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
  If LoanFound Is Nothing Then
    'Put your code here for MsgBox/Delete etc

  End If
Next cell
Thank you for the reply. This code gives me a subscript out of range error, the debugger stops at this line:
.ListObjects(1).AutoFilter.ShowAllData
I am not using official tables, I am simplys using filters
 
Upvote 0
I tried something like this but the debugger stopped at Set rngData = .Range("C11:C" & .Range("C" & Rows.Count).End(xlUp).Row).Value2 with an object not found error.

VBA Code:
Sub Delete_Closed_Loans()
ActiveSheet.Unprotect Password:="Mortgage1"
Dim cell As Range, rngData As Range, rngMain As Range, LoanFound As Range

With Sheets("Data")
'  .ListObjects(1).AutoFilter.ShowAllData
  Set rngData = .Range("C11:C" & .Range("C" & Rows.Count).End(xlUp).Row).Value2
End With
With Sheets("Main")
   Set rngMain = .Range("A8:A" & .Range("A" & Rows.Count).End(xlUp).Row).Value
End With

For Each cell In rngMain
  Set LoanFound = rngData.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
  If LoanFound Is Nothing Then
    'Put your code here for MsgBox/Delete etc
    MsgBox "Hello " & LoanFound
  
  End If
Next cell
End Sub
 
Upvote 0
I tried something like this but the debugger stopped at Set rngData = ...
Yes, that line is incorrect because you are trying to set a range equal to a value. Similar for the other range.
Also, for the Find method to work, the value being searched for must be visible so we do need to make sure that the 'Data' sheet is not filtered, so I have put an equivalent line back in the code.
Your MsgBox would also produce an error for the Loan numbers not found.

Try these changes
Rich (BB code):
Sub Delete_Closed_Loans_v2()
  ActiveSheet.Unprotect Password:="Mortgage1"
  Dim cell As Range, rngData As Range, rngMain As Range, LoanFound As Range
  
  With Sheets("Data")
    If .FilterMode Then .ShowAllData
    Set rngData = .Range("C11:C" & .Range("C" & Rows.Count).End(xlUp).Row).Value2
    Set rngData = .Range("C11:C" & .Range("C" & Rows.Count).End(xlUp).Row)
  End With
  With Sheets("Main")
     Set rngMain = .Range("A8:A" & .Range("A" & Rows.Count).End(xlUp).Row).Value
     Set rngMain = .Range("A8:A" & .Range("A" & Rows.Count).End(xlUp).Row)
  End With
  
  For Each cell In rngMain
    Set LoanFound = rngData.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
    If LoanFound Is Nothing Then
      'Put your code here for MsgBox/Delete etc
      MsgBox "Hello " & LoanFound
      MsgBox cell.Value & " loan not found"
    End If
  Next cell
End Sub
 
Upvote 0
Yes, that line is incorrect because you are trying to set a range equal to a value. Similar for the other range.
Also, for the Find method to work, the value being searched for must be visible so we do need to make sure that the 'Data' sheet is not filtered, so I have put an equivalent line back in the code.
Your MsgBox would also produce an error for the Loan numbers not found.

Try these changes
Rich (BB code):
Sub Delete_Closed_Loans_v2()
  ActiveSheet.Unprotect Password:="Mortgage1"
  Dim cell As Range, rngData As Range, rngMain As Range, LoanFound As Range
 
  With Sheets("Data")
    If .FilterMode Then .ShowAllData
    Set rngData = .Range("C11:C" & .Range("C" & Rows.Count).End(xlUp).Row).Value2
    Set rngData = .Range("C11:C" & .Range("C" & Rows.Count).End(xlUp).Row)
  End With
  With Sheets("Main")
     Set rngMain = .Range("A8:A" & .Range("A" & Rows.Count).End(xlUp).Row).Value
     Set rngMain = .Range("A8:A" & .Range("A" & Rows.Count).End(xlUp).Row)
  End With
 
  For Each cell In rngMain
    Set LoanFound = rngData.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
    If LoanFound Is Nothing Then
      'Put your code here for MsgBox/Delete etc
      MsgBox "Hello " & LoanFound
      MsgBox cell.Value & " loan not found"
    End If
  Next cell
End Sub
Thanks, this worked great, one last thing, I am trying to identify the row so I can delete row if duplicate. I have modified the code below but need to know how to identify the row. The part of the code below where it says Selection.EntireRow.Delete should have the row number rather than the word selection but I don't see how to identify the row with the duplicate.

VBA Code:
Sub Delete_Closed_Loans()
ActiveSheet.Unprotect Password:="Mortgage1"
Dim cell As Range, rngData As Range, rngMain As Range, LoanFound As Range
Dim MSG1 As String

With Sheets("Data")
  Set rngData = .Range("C11:C" & .Range("C" & Rows.Count).End(xlUp).Row)
End With
With Sheets("Main")
   Set rngMain = .Range("A8:A" & .Range("A" & Rows.Count).End(xlUp).Row)
End With

For Each cell In rngMain
  Set LoanFound = rngData.Find(What:=cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
  If LoanFound Is Nothing Then
    MSG1 = MsgBox(cell.Value & " " & cell.Offset(0, 1).Value & " loan not found, delete?", vbYesNoCancel)
    If MSG1 = vbYes Then
        Selection.EntireRow.Delete
    ElseIf MSG1 = vbNo Then
    Else
    Exit Sub
    End If
  End If
Next cell
End Sub
 
Upvote 0
1. If you are going to be deleting rows on your way through then you need to work from the bottom up or you will likely miss even checking some rows.

For example, if you are checking cells A8:A10 and when checking A8 you decide to delete that row, A9 moves up to A8 but your have already checked A8 so now we check A9 (which was A10 but has moved up). Therefore we have missed checking what was originall in A9.

2. I not that you appear to be disregarding my advice about making sure the data is not filtered. Assuming you want to check all values in column A (from row 8) of Main against all values in column C (from row11) of Data then both sheets should be confirmed to be unfiltered before running this code otherwise values will at some stage be missed!

3. To delete the row relating to the MsgBox use
VBA Code:
cell.EntireRow.Delete
 
Upvote 0

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

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