简体   繁体   中英

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. If the cell string does not contain : "(DELETED)", I want the entire row to be 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. 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.

As Tom mentioned, it returns an integer, so it'd be easier doing something like this, rather than using a 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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