I have tried many variations of code and this has got me the closest.I have 2 worksheets; worksheet1 has total data, worksheet2 has a range which identifys the rows in sheet1 need to be retained, so any value not found in that range should be deleted. When I run the code what was deleted didn't match up, and in a small test it didn't delete any values. What I am I doing wrong?
Sub Delete()
Dim nRng As Range, rng As Range
Set nRng = Range("Dog") 'Substitute actual name of range
Set rng = Range("A1", ActiveSheet.Cells(Rows.Count, "A").End(xlUp))
For Each c In rng 'Look at each item in col K
If WorksheetFunction.CountIf(nRng, c.Value) = 0 Then 'Evaluate against named range
c.EntireRow.Delete 'Delete rows with no match to named range
End If
Next
End Sub
Aucun commentaire:
Enregistrer un commentaire