如果单元格值不是以 cioi、600t 和 htk4 开头,则删除所有行

delete all rows if the cell value does not start with cioi, 600t and htk4

我必须删除除第 4 列中以 cioi、600t 和 htk4 开头的行以外的所有行。我的 excel 也有 headers,不应删除。将来这个列表我在那种情况下的增加我可以添加到我的宏

试试这个代码:

'find last populated row
xEndRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

'iterate all rows from bottom to top
For I = xEndRow To 2 Step -1
   'get value of cell at current row and 4th column
   currentCellValue = ActiveSheet.Cells(I, 4).Value
   'get first 4 characters
   startsWith = Left(currentCellValue, 4)

   'if row doesnt met criteria, delete it
   If startsWith <> "cioi" And startsWith <> "600t" And startsWith <> "htk4" Then
       Rows(I).Delete
   End If

Next

您可能想让它更具动态性,因为由于缺乏相关细节,所有内容都是硬编码的。


无论如何,这是我之前测试数据的图像:

在 运行 宏之后: