2017-07-10 429 views
0

Green file的顏色,當條件滿足

什麼代碼的作用是,如果上校X和Y包含日期整行,顏色全行作爲黃色。
如果只有X包含日期,則將整行着色爲紅色。
但是,如果X和Y爲空,則將其變爲綠色。在最後的條件。

如果滿足條件,我不能讓我的代碼在整行上着色。

Dim i As Long 
Dim lrX As Long  'last row with a filled cell in column X 
Dim lrY As Long  'last row with a filled cell in column Y 
Dim lr As Long  'max of lrX and lrY 
Dim ws As Worksheet 

Set ws = ActiveSheet 

lrX = Range("X" & Rows.Count).End(xlUp).Row 
lrY = Range("Y" & Rows.Count).End(xlUp).Row 
lr = Application.WorksheetFunction.Max(lrX, lrY) 

For i = 2 To lr  'my data starts in row 1, otherwise change this 
    If IsDate(ws.Range("X" & i).Value) Then 
     If IsDate(ws.Range("Y" & i).Value) Then 
      ws.Range("a" & i).EntireRow.Interior.Color = vbYellow  'both X and Y have a date value, so Yellow 
     Else 
      ws.Range("a" & i).EntireRow.Interior.Color = vbRed  'only X has a date 
     If (.Cells(i, 24).Value = "") And _ 
     (.Cells(i, 25).Value = "") Then 
     .Rows(i).EntireRow.Interior.ColorIndex = 4 ' 4: Green 
     End If 
    End If 

Next i 

回答

0

你也可以做這樣的事情用一個公式。你可以修改這個函數來處理更多的邏輯和顏色。

如果表達式爲真,這將使行變綠。您將顏色指定爲RGB。

=ColorRowIF(A1=B1, 1, 181, 0)

或者在你的情況

=ColorRowIF(IsDate(A1), 1, 181, 0)

Public Function ColorRowIF(Condition As Boolean, r As Integer, g As Integer, b As Integer) As String 
    Dim row As Integer 
    row = Application.Caller.row 

    If Condition = True Then 
     ActiveSheet.Evaluate "ColorRow(" & row & ", " & r & ", " & g & ", " & b & ")" 
    Else 
     'ws.Rows(r).Interior.Color = vbRed 
    End If 

    ColorRowIF = Condition 
End Function 


Public Sub ColorRow(row As Integer, r As Integer, g As Integer, b As Integer) 

    Dim ws As Worksheet 
    Set ws = ActiveSheet 
    ws.Rows(row).Interior.Color = RGB(r, g, b) 

End Sub 


Function IsDate(CellDate As Date) As Boolean 
    If CellDate >= 1 And CellDate <= #12/31/2199# Then 
    ' 1 is equal to January 1, 1900 
     IsDate = True 
    Else 
     IsDate = False 
     End If 
End Function 
+0

謝謝Michael! – jackie

0

幾個原因你的代碼沒有工作:

  1. 你有If (.Cells(i, 24).Value = "") And (.Cells(i, 25).Value = "") Then但你缺少的ws對象的With聲明。
  2. 您有3 x If且只有2 x End If
  3. 您有If IsDate(ws.Range("X" & i).Value) Then作爲第一個If條件,所以如果列「X」中的值不是日期,則會離開所有循環(因爲只有先通過了這個循環纔會檢查其他2 x IfIf)。

嘗試(代碼註釋中說明)下面的代碼:

Dim i As Long 
Dim lrX As Long  'last row with a filled cell in column X 
Dim lrY As Long  'last row with a filled cell in column Y 
Dim lr As Long  'max of lrX and lrY 
Dim ws As Worksheet 

Set ws = ActiveSheet 

lrX = Range("X" & Rows.Count).End(xlUp).Row 
lrY = Range("Y" & Rows.Count).End(xlUp).Row 
lr = Application.WorksheetFunction.Max(lrX, lrY) 

With ws ' <-- added the with statement 
    For i = 2 To lr  'my data starts in row 1, otherwise change this 
     ' first check if both cells are empty 
     If .Range("X" & i).Value = "" And .Range("Y" & i).Value = "" Then 
      .Rows(i).Interior.ColorIndex = 4 ' 4: Green 
     ElseIf IsDate(.Range("X" & i).Value) Then 
      If IsDate(.Range("Y" & i).Value) Then 
       .Rows(i).Interior.Color = vbYellow 'both X and Y have a date value, so Yellow 
      Else 
       .Rows(i).Interior.Color = vbRed 'only X has a date 
      End If 
     End If 
    Next i 
End With 
+0

謝謝曬! – jackie