如果find内容,则删除整行

如果单元格以文本“Joe Smith”或“Jim Bob”开头,我想删除整行。 这是我从另一个问题,但需要修改的代码:

Sub SimpleDeletingMechanism() Dim c As Range ' loop through all cells in range A1:A + last userd Row in column A For Each c In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row) ' when Joe Smith is found in cell's content If InStr(1, c.Text, "Joe Smith", vbTextCompare) > 0 Then ' delete that row ' when Jim Bob is found in cell's content ElseIf InStr(1, c, "Jim Bob", vbTextCompare) > 0 Then ' delete that row End If Next End Sub 

任何人都可以帮我填补删除行的差距,如果其中一个名字被发现吗?

无论何时从集合中删除对象,都必须向后迭代,否则,当集合被重新索引时,最终会“跳过”一些元素。

 Sub SimpleDeletingMechanism() Dim rng as Range Dim c As Range Dim i as Long Set rng = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row) For i = rng.Rows.Count to 1 Step -1 '// Iterate BACKWARDS over the collection Set c = rng.Cells(i,1) ' when Joe Smith is found in cell's content If InStr(1, c.Text, "Joe Smith", vbTextCompare) > 0 Then c.EntireRow.Delete ' when Jim Bob is found in cell's content ElseIf InStr(1, c, "Jim Bob", vbTextCompare) > 0 Then c.EntireRow.Delete End If Next End Sub