简体   繁体   English

如果Excel行在单元格中不包含特定字符串,则将其删除

[英]Delete Excel Rows if They Do Not Contain a Specific String in a Cell

I have an excel file where on the 3d (C) column cells, there is a text. 我有一个Excel文件,其中3d(C)列单元格上有一个文本。 If the cell string does not contain : "(DELETED)", I want the entire row to be deleted. 如果单元格字符串包含:“(DELETED)”,我希望删除整行。 I tried the following code and it works but as far as concerned the syntax, it needs to be edited. 我尝试了以下代码,它可以工作,但就语法而言,需要对其进行编辑。

Excel文件的打印屏幕

Sub sbDelete_Rows_IF_Cell_Contains_Specific_String()
 Dim lRow As Long
 Dim iCntr As Long
 lRow = 5
  For iCntr = lRow To 1 Step -1
    celltxt = Cells(iCntr, 3).Value
    If InStr(1, celltxt, "(DELETED)") Then

    Else
     Rows(iCntr).Delete
    End If
  Next
End Sub

If the script finds the cells that contain the string : "(DELETED)", then the program will not do anything. 如果脚本找到包含字符串“(DELETED)”的单元格,则程序将不执行任何操作。 It will delete the ones that do not contain this string. 它将删除不包含此字符串的字符串。 The syntax is bad and I wonder how I can combine the "InStr" and the "IF NOT" functions. 语法不好,我不知道如何组合“ InStr”和“ IF NOT”功能。

As Tom mentioned, it returns an integer, so it'd be easier doing something like this, rather than using a NOT 正如Tom所提到的,它返回一个整数,所以这样做比使用NOT会更容易

Sub test()
Dim lrow As Integer
lrow = Cells(Rows.Count, "A").End(xlUp).Row

For i = lrow To 1 Step -1
    If InStr(1, Cells(i, 3), "(DELETED)") = 0 Then
        Rows(i).EntireRow.Delete
    End If
Next
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM