Delete row if value found in list from another workbook

jack3427dsg546

New Member
Joined
Aug 17, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I based my code off of this but I can't get my code to work. The MsgBox pops up at the end but no rows are actually deleted.
Column A of Report is the Invoice Number (alphanumeric). Column C of Invoices are the invoice numbers we already have in the system. Therefore, I want to delete the rows in Report that where Report.ColumnA matches Invoices.ColumnC. Any help would be appreciated.

VBA Code:
Sub t()
Dim source As Worksheet, dest As Worksheet, i As Long, fn As Range, flg As Boolean
Set source = Workbooks("Invoices.xls").Sheets(1)
Set dest = Workbooks("Report.xlsx").Sheets(1)
    For i = dest.Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        Set fn = source.Columns("C").Find(dest.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 Dest", vbInformation, "ITEMS DELETED"
End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I got an answer from Remove rows from another list using excel VBA - Help!!!
VBA Code:
Public Sub delete_Selected_Rows()
' Search through column A of 'Report'
' and delete the entire row if a value matches that found in column C of 'Invoices' 


Dim rng1 As Range, rng2 As Range, rngToDel As Range, c As Range
Dim lastRow As Long


  With Workbooks("Report.xlsx").Sheets(1)
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set rng1 = .Range("A2:A" & lastRow)
  End With
  With Workbooks("Invoices.xlsx").Sheets(1)
    lastRow2 = .Cells(.Rows.Count, "C").End(xlUp).Row
    Set rng2 = .Range("C9:C" & lastRow2)
    End With

  For Each c In rng1
  ' if value from rng1 is found in rng2 then
  ' build range to Delete ...
    If Not IsError(Application.Match(c.Value, rng2, 0)) Then
      If rngToDel Is Nothing Then
        Set rngToDel = c
      Else
        Set rngToDel = Union(rngToDel, c)
      End If
    End If
  Next c
  
If Not rngToDel Is Nothing Then rngToDel.EntireRow.Delete


End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,011
Messages
6,122,677
Members
449,092
Latest member
tayo4dgacorbanget

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