"If..Then" 循环突出显示行

"If..Then" loop to highlight rows

我有循环遍历大量数据的代码,复制 B 列中包含短语 Flowing 的任何行,并将该行粘贴到另一个工作表中。代码是:

Sub HighlightFlowingIntervals()

Worksheets("3901").Activate

Dim i As Long
Dim cell As Range
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("3901")
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Sheet2")

For i = 6 To ws1.Range("C17300").End(xlUp).Row
    If ws1.Cells(i, "B").Value = "Flowing" Then ws1.Rows(i).Copy ws2.Rows(ws2.Cells(ws2.Rows.Count, 2).End(xlUp).Row + 1)
Next i

End Sub

我现在想使用相同的循环结构,但不是复制和粘贴每一行,而是简单地突出显示通过 "If..Then" 标准的每一行。我知道如何使用宏记录器突出显示,但除非万不得已,否则我不想这样做。我只是不确定如何在循环中实现突出显示。

只使用条件格式。

  1. Select 您要检查的所有单元格
  2. Select "Conditional Formatting..." > "Highlight Cells Rules..." > "Text that contains..."
  3. 输入"Flowing"
  4. 选择高亮颜色
  5. 单击 "OK",然后观察包含您的文本的单元格亮起!

您需要拆分 then

Sub Button1_Click()


    Dim i As Long
    Dim cell As Range
    Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("3901")
    Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets("Sheet2")
    With ws1
        For i = 6 To .Cells(.Rows.Count, "C").End(xlUp).Row
            If .Cells(i, "B").Value = "Flowing" Then
                .Rows(i).Copy ws2.Rows(ws2.Cells(ws2.Rows.Count, 2).End(xlUp).Row + 1)
                .Range(.Cells(i, "A"), .Cells(i, "D")).Interior.Color = vbYellow
            End If
        Next i
    End With
End Sub