2016-08-26 52 views
-1

如何编辑我的代码以仅突出显示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.Range("G:K").Value.Interior.ColorIndex = 3

更换With cell.EntireRow.Interior请问这是我当前的代码不工作就是我的意思是,我试图做

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 
+0

你有没有考虑条件格式呢? – pnuts

回答

1

你引用应该是

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

您可以查看我的编辑请我不够清楚 – phillipsK

+0

请参阅编辑@phillipsK –

+0

这是行不通的。 – phillipsK

相关问题