2009-12-15 14 views
0

我有一個工作表,其中包含一個任務列表,每行一個。列A是任務名稱,列B是必須完成的日期,列C是必須完成的日期。 D列用於指示何時完成。如果此列包含任何內容,則整行的背景顏色應爲灰色,否則應爲白色。如何爲條件格式使用Worksheet_Change事件?

我在想worksheet_change事件是處理這個事件的最好方法。我想我可以使用條件格式化,但是如果單元格被拖動,這似乎很容易破壞 - 我需要這樣做,以儘可能「防彈」!

在僞代碼中,我試圖實現以下目標:

Private Sub Worksheet_Change(ByVal Target As Range) 
    If Target includes a cell in column "D" 
     If "D" is not empty 
      Set entire row background to grey 
     Else 
      Set entire row background to white 
     End If 
    End If 
End Sub 

任何人可以給我約來實現這一點的最好辦法任何指針?我甚至在正確的路線上,還是有更好的方法?

回答

1

我想你可以使用下列條件對每一個細胞:

=INDIRECT("$D" & ROW())>0 

我做了一些複製/粘貼和拖動周圍的細胞和條件格式沒有突破。

0

使用條件格式:

轉到Tools->Options->General並激活R1C1 reference style

條件:=ISEMPTY(RC4)

用VBA:

Private Sub Worksheet_Change(ByVal Target As Range) 
Dim found As Range 
Dim cell As Range 
Dim colStart As Integer 
Dim colEnd As Integer 

    Set found = Intersect(Target, Me.Columns(4)) 
    colStart = 1 
    colEnd = 4 

    If Not found Is Nothing Then 
     For Each cell In found 
      With Me.Range(Me.Cells(cell.Row, colStart), Me.Cells(cell.Row, colEnd)).Interior 
       If IsEmpty(cell) Then 
        .ColorIndex = 15 
       Else 
        .ColorIndex = xlNone 
       End If 
      End With 
     Next cell 
    End If 

    Set found = Nothing 
End Sub 

我建議使用電導率itional格式化