突出显示一行中的几列和单元格

时间:2016-08-26 16:20:39

标签: excel vba excel-vba

我怎么能编辑我的代码只是突出显示G:K列中的行而不是浪费内存和时间突出显示整行?

With ActiveSheet    'set this worksheet properly!
    'lastrow = .cells(Rows.Count, 1).End(xlUp).Row
    lastrow = Range("K6500").End(xlUp).Row - 2
    For Each cell In .Range("K3:K" & lastrow)
        If cell = "Wrong Date" Then
          'With cell.EntireRow.Interior
           With cell.Range("G:K").Value.Interior.ColorIndex = 3

                Rows().Select
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 3937500
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

我当前的代码无效,因为我尝试将With cell.EntireRow.Interior替换为With cell.Range("G:K").Value.Interior.ColorIndex = 3

请原谅我这就是我想要做的事情

Sub highlight_wrong_Date()

Dim Rng As Range, lCount As Long, lastrow As Long
Dim cell As Object

With ActiveSheet    'set this worksheet properly!

    lastrow = Range("K6500").End(xlUp).Row - 2
    For Each cell In .Range("K3:K" & lastrow)
        If cell = "Wrong Date" Then

            With cell.Range(.cells(cell.Row, "G"), .cells(cell.Row, "K"))
                Rows().Select
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 3937500
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

        ElseIf cell = "Pass" Then
             With cell.Range(.cells(cell.Row, "G"), .cells(cell.Row, "K"))
                Rows().Select
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 61046
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

        Else
            With cell.EntireRow.Interior
                Rows().Select
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

        End If

     Next cell
End With


End Sub

但我收到错误,说单元格对象不支持此功能。如果单元格在列O中具有“错误日期”或“通过”值,则我想分别突出显示红色或绿色。

第3次修改

Sub highlight_wrong_Date()

Dim Rng As Range, lCount As Long, lastrow As Long
Dim cell_value As Object

With ActiveSheet    'set this worksheet properly!

    lastrow = Range("K6500").End(xlUp).Row - 2
    For Each cell_value In .Range("K3:K" & lastrow)
        If cell_value = "Wrong Date" Then

            With .Range(.cells(cell.Row, "G"), .cells(cell.Row, "K"))
                'Rows().Select
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 3937500
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

        ElseIf cell_value = "Pass" Then
             With .Range(.cells(cell.Row, "G"), .cells(cell.Row, "K"))
                'Rows().Select
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .Color = 61046
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

        Else
            With cell.EntireRow.Interior
                Rows().Select
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

        End If

     Next cell_value
End With




End Sub

1 个答案:

答案 0 :(得分:1)

您的推荐应该是

With .Range("G" & cell.Row & ":K" & cell.Row)