“If..Then”循环突出显示行

时间:2015-10-22 20:19:25

标签: excel vba loops if-statement highlight

我的代码循环遍历大量数据,复制包含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”条件的每一行。我知道如何突出使用宏录制器,但除非必须,否则不想这样做。我只是不确定如何在循环中实现突出显示。

2 个答案:

答案 0 :(得分:1)

只需使用条件格式。

  1. 选择要检查的所有单元格
  2. 选择“条件格式...”> “突出显示单元格规则...”> “包含......的文字”
  3. 输入“流动”
  4. 选择突出显示颜色
  5. 点击“确定”,然后观看包含文字的单元格!

答案 1 :(得分:0)

您需要拆分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