【发布时间】:2018-04-18 07:34:54
【问题描述】:
我有一个用 VBA 编写的代码,它将检查日期并根据它用适当的颜色填充背景。
我有单元格(A 到 G)。
我想检查 G 是否为空,如果是,我想更改单元格 (A)(B)(D)(E)(F)(G) 的背景颜色,除了 CELL (C)。
代码:
Private Sub CommandButton1_Click()
Dim i As Long
For i = Range("C5000").End(xlUp).Row To 2 Step -1 'Range upto 5000, chnge this as per your requirment'
If IsEmpty(Cells(i, 3)) Then
Range("A" & i & ":G" & i).Interior.Color = xlNone
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 0 Then
Cells(i, 3).Interior.Color = vbGreen
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) = 0 Then
Cells(i, 3).Interior.Color = vbYellow
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 1 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 4 Then
Cells(i, 3).Interior.Color = vbRed
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) >= 5 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) <= 10 Then
Cells(i, 3).Interior.Color = vbCyan
Else
Cells(i, 3).Interior.ColorIndex = xlNone
End If
' your 2nd criteria to color the entire row if "F" is not empty
If Trim(Range("G" & i).Value) <> "" Then
Range("A" & i & ":G" & i).Interior.ColorIndex = 15
ElseIf Trim(Range("G" & i).Value) = "" Then
Range("A" & i & ":B" & i).Interior.Color = RGB(255, 189, 189)
Range("D" & i & ":G" & i).Interior.Color = RGB(255, 189, 189)
End If
Next
End Sub
【问题讨论】:
标签: excel if-statement colors vba