2016-08-26 83 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

相關問題