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



i有一个Excel表,其中C列的某些单元格包含" RINSE"一词(其他单元格具有不同的其他内容)。

使用VBA代码,这是我将如何选择包含"冲洗"一词的所有行 - 此代码正常工作。

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

但是,我想完全相反,即选择所有不包含"冲洗"一词的行。我已经尝试了以下操作,但它不起作用。

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

如果可以在您的单元格值中的任何地方找到"冲洗"

您可以滤除冲洗值,然后选择可见的单元格。
比查看每个单独的单元更快。

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

顺便说一句,您可以使用此代码的变体同时选择多个行(就像您可以使用CTL 单击一样),例如,所有包含" RINSE"一词的行。

@Renee-更改下面所示的条件线。

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

最新更新