如何自动过滤或类似只包含某些词

时间:2021-07-27 20:04:41

I understand I can use auto filter like this to remove certain rows that contain certain strings, as shown below (in this example the removed row contains string "111-11111").

我理解我可以像这样使用自动过滤器来删除包含特定字符串的某些行,如下所示(在本例中,删除的行包含字符串“111-11111”)。

Set ws = Sheets("Gate_Results")
Set rng1 = ws.Range(ws.[u1], ws.Cells(Rows.Count, "U").End(xlUp))
With ActiveSheet
    .AutoFilterMode = False
    rng1.AutoFilter Field:=1, Criteria1:="111-11111"
    rng1.Offset(1, 0).EntireRow.Delete
    .AutoFilterMode = False
End With

However I was wondering if there was a way to remove strings that DO NOT contain the criteria. An example of this would be if string ###-##### represents product codes, if I want to filter my list to see only 3 different products out of a library of codes, could I use the AutoFilter function to do so, and if so how?

但是,我想知道是否有一种方法可以删除不包含条件的字符串。如果我想要过滤我的列表,从一个代码库中只看到3个不同的产品,那么我可以使用AutoFilter函数来这样做吗?如果是这样的话,那该怎么办呢?

I tried the following (and similar example but &-ing in additional strings in the criteria1 section) already, however it did not work at all, but that was just my amateurish guesswork.

我已经尝试了下面的例子(和类似的例子,但是在准则部分的附加字符串),但是它根本不起作用,但是那只是我的业余猜测。

Set ws = Sheets("Gate_Results")
Set rng1 = ws.Range(ws.[u1], ws.Cells(Rows.Count, "U").End(xlUp))
With ActiveSheet
    .AutoFilterMode = False
    rng1.AutoFilter Field:=1, Criteria1:<>"111-11111"
    rng1.Offset(1, 0).EntireRow.Delete
    .AutoFilterMode = False
End With

I'd appreciate any help on finding a working method, even if it doesn't involve autofilter. But efficiency would be extra appreciated. :D

如果您能帮助我找到一个有效的方法,我将不胜感激,即使它不涉及自动过滤器。但是效率将会得到额外的赞赏。:D

1 个解决方案

#1


1  

You need to put the <> between the inverted commas. So:

您需要将<>放在引号之间。所以:

Set ws = Sheets("Gate_Results")
Set rng1 = ws.Range(ws.[u1], ws.Cells(Rows.Count, "U").End(xlUp))
With ActiveSheet
    .AutoFilterMode = False
    rng1.AutoFilter Field:=1, Criteria1:"<>111-11111"
    rng1.Offset(1, 0).EntireRow.Delete
    .AutoFilterMode = False
End With

#1


1  

You need to put the <> between the inverted commas. So:

您需要将<>放在引号之间。所以:

Set ws = Sheets("Gate_Results")
Set rng1 = ws.Range(ws.[u1], ws.Cells(Rows.Count, "U").End(xlUp))
With ActiveSheet
    .AutoFilterMode = False
    rng1.AutoFilter Field:=1, Criteria1:"<>111-11111"
    rng1.Offset(1, 0).EntireRow.Delete
    .AutoFilterMode = False
End With