如何select不包含某个单词的单元格

我有一个Excel表,其中C列的一些单元格包含单词“冲洗”(其他单元格有其他各种内容)。

使用VBA代码,这里是我将如何select在C列中包含单词“冲洗”的所有行 – 此代码工作正常。

For i = 3 To 300 If Cells(i, 3).Value = "Rinse" Then Rows(i).Select Selection.FormatConditions.Delete End If Next 

不过,我想要做的恰恰相反,即selectC列中不包含单词“Rinse”的所有行 。 我已经尝试了以下,但它不起作用。

 For i = 3 To 300 If Cells(i, 3).Value = Not "Rinse" Then Rows(i).Select Selection.FormatConditions.Delete End If Next 

我怎么做到这一点?

使用Instr函数,如下所示:

 If Instr(Cells(i, 3).Value, "Rinse") = 0 Then 

改变你的代码这一行(<>不等于)

  If Cells(i, 3).Value <> "Rinse" Then 

Like运算符在这里很有用:

 If Not Cells(i, 3).Value Like "*Rinse*" Then 

如果“漂洗”可以在您的细胞价值的任何地方find

您可以过滤出冲洗值,然后select可见的细胞。
可能比看每个单元格更快。

 Public Sub Test() Dim lRow As Long With ThisWorkbook.Worksheets("Sheet1") lRow = .Cells(.Rows.Count, 3).End(xlUp).Row With .Range(.Cells(1, 3), .Cells(lRow, 3)) .AutoFilter Field:=1, Criteria1:="<>*Rinse*" 'Can replace Select in next row with .FormatConditions.Delete .SpecialCells(xlCellTypeVisible).Select End With .ShowAllData End With End Sub 

这个代码的优点在于速度。 加速是通过对每一行只引用一次而对结果只引用一次来实现的,并且只对所使用的范围列进行格式化,而不是对整行进行格式化。

 Private Sub SelectNonContiguousRange() Dim RngAddress() As String Dim i As Long Dim R As Long ReDim RngAddress(300) ' this number should be With ActiveSheet For R = 3 To 300 ' equal to this number ' use = (equal) or <> (unequal) as required: If .Cells(R, "C").Value <> "Rinse" Then ' If .Cells(R, "C").Value = "Rinse" Then RngAddress(i) = .Range(.Cells(R, "A"), _ .Cells(R, .UsedRange.Columns.Count)).Address i = i + 1 End If Next R ReDim Preserve RngAddress(i - 1) .Range(Join(RngAddress, ",")).FormatConditions.Delete End With End Sub 

顺便说一句,您可以使用此代码的变体来同时select多行(例如您可以使用Ctl + Click),例如包含单词“冲洗”的所有行。

@Renee – 更改if条件行,如下所示。

 For i = 3 To 300 If Cells(i, 3).Value <> "Rinse" Then Rows(i).Select Selection.FormatConditions.Delete End If Next