Hi folks, i'm getting a Run-Time Error '1004' "Delete method of Range class failed" for the following code.
The debug shows the line
to be the failure point.
What should happen is each row on 'Master Sheet' is checked for a corresponding row on 'Import' Sheet', if no matching row is found on the 'Import Sheet' then the row on 'Master Sheet' is deleted.
Currenty, the code bugs out when the first non-matching row is found.
***n.b. it is possible for there to different numbers of rows between the two worksheets.***
Code:
Sub Satisfied()
Dim MasterSht As Worksheet
Dim ImportSht As Worksheet
Dim LastRow As Long
Dim x As Long
Application.ScreenUpdating = False
Set MasterSht = ThisWorkbook.Sheets("Master Sheet")
Set ImportSht = ThisWorkbook.Sheets("Import Sheet")
With MasterSht
LastRow = .Cells(.Rows.Count, "G").End(xlUp).Row
For x = LastRow To 9 Step -1 ' always loop backwards when deleting rows
If IsError(Application.Match(.Range("G" & x).Value, ImportSht.Range("G:G"), 0)) And _
IsError(Application.Match(.Range("L" & x).Value, ImportSht.Range("L:L"), 0)) Then 'check if there is no match
.Rows(x).Delete ' delete row if no match
End If
Next x
End With
Application.ScreenUpdating = True
End Sub
The debug shows the line
Code:
.Rows(x).Delete
What should happen is each row on 'Master Sheet' is checked for a corresponding row on 'Import' Sheet', if no matching row is found on the 'Import Sheet' then the row on 'Master Sheet' is deleted.
Currenty, the code bugs out when the first non-matching row is found.
***n.b. it is possible for there to different numbers of rows between the two worksheets.***
Last edited: