查找部分字符串匹配的VBA代码

时间:2018-07-26 16:12:48

标签: vba excel-vba

我正在寻找使用经过修改的代码,以便找到包含“滥用忽略”字样的单元格(例如部分匹配项)-然后删除该行。任何帮助表示赞赏!

Sub TakeOutAllOtherCourses()
Last = Cells(Rows.Count, "D").End(xlUp).Row
For i = Last To 1 Step -1
    If (Cells(i, "D").Value) = "Abuse Neglect" Then
'Cells(i, "A").EntireRow.ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
        Cells(i, "A").EntireRow.Delete
    End If
Next i

3 个答案:

答案 0 :(得分:1)

尝试输入

Sub TakeOutAllOtherCourses()
last = Cells(Rows.Count, "D").End(xlUp).Row
For i = last To 1 Step -1
    If InStr(Cells(i, "D").Value, "Abuse Neglect") > 0 Then
        Cells(i, "A").EntireRow.Delete
    End If
Next i
End Sub

答案 1 :(得分:1)

检查Like运算符在此link (Like Operator - Visual Basic)中的工作方式

代码:

Sub TakeOutAllOtherCourses()
Last = Cells(Rows.Count, "D").End(xlUp).Row
For i = Last To 1 Step -1
    If (Cells(i, "D").Value) Like "Abuse Neglect*" Then
'Cells(i, "A").EntireRow.ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
        Cells(i, "A").EntireRow.Delete
    End If
Next i
End Sub

答案 2 :(得分:0)

如果您的D列中没有空白,这是一种既可以自动过滤也不可以循环的(但未经测试)的方式:

Sub TakeOutAllOtherCourses()
    With Range("D1", Cells(Rows.Count, "D").End(xlUp))
        .Replace What:="Abuse Neglect*", Replacement:="", LookAt:=xlPart
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With
End Sub
相关问题