所以我有这个代码,它的作用是它在列上找到单词“Hello”,如果在“J”列上找到该String,整个行将被删除,但我想保留所有行即使单元格中同时包含“Hello”和“World”,其中也包含“World”一词。例如:
第J栏
1你好世界
2你好的人
3大家好
4你好的人
5你好世界
6大家好
7你好人
8 Hello world
唯一应该保留的行是1,5和8.谢谢,任何帮助将不胜感激。
Sub Test()
Dim c As Range
Dim SrchRng
Set SrchRng = ActiveSheet.Range("J1", ActiveSheet.Range("J65536").End(xlUp))
Do
Set c = SrchRng.Find("Hello", LookIn:=xlValues)
If Not c Is Nothing Then c.EntireRow.Delete
Loop While Not c Is Nothing
End Sub
答案 0 :(得分:0)
我通常建议您在删除行时使用For
循环,从最后开始并向上工作。
这应该适合你:
Sub test2()
Dim c As Range, SrchRng As Range
Dim ws as Worksheet
Set ws = ActiveSheet
Set SrchRng = ws.Range("J1", ws.Range("J65536").End(xlUp))
Dim i As Long, lastRow As Long
lastRow = SrchRng.Rows(SrchRng.Rows.Count).Row
With ws
For i = lastRow To 1 Step -1
If InStr(1, .Cells(i, 10), "Hello") >= 1 And InStr(1, .Cells(i, 10), "world") = 0 Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
End Sub