【发布时间】:2017-07-10 02:40:13
【问题描述】:
代码的作用是如果 Col 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
【问题讨论】: