【问题标题】:VBA code for finding a partial string match用于查找部分字符串匹配的 VBA 代码
【发布时间】:2018-07-26 16:12:47
【问题描述】:

我希望使用此代码,但已对其进行了修改,以便它找到包含 Abuse Neglect (如部分匹配)字样的任何单元格 - 然后删除该行。任何帮助表示赞赏!

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

【问题讨论】:

标签: vba excel


【解决方案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

【讨论】:

    【解决方案2】:

    检查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
    

    【讨论】:

    • 太完美了!!正是我要找的 - 我只是不知道如何添加“喜欢”你太棒了!
    • @Andrew Upvote 或接受答案,如果此/任何答案对您/对您有帮助,以造福未来的读者。见:stackoverflow.com/help/why-vote
    【解决方案3】:

    如果您的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2022-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      相关资源
      最近更新 更多