我有檢查輸入的日期與當前的日期,並填寫細胞在適當的顏色,並檢查colomn 「F」沒有清空將色彩VBA代碼D,E,F列。哪裏是錯誤在第二個IF語句
問題是我到目前爲止有21條記錄,但系統只是彩色19條記錄,所以2行在F列中不是空的。
代碼:
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
Cells(i, 3).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("F" & i).Value) <> "" Then Range("D" & i & ":F" & i).Interior.ColorIndex = 15
Next
End Sub
爲什麼使用VBA?看起來像是'Conditional formatting'的工作。 –
是的,我知道,但我想使用vba –
代碼看起來沒問題。使用'debug.print len(Range(「F」&i).Value)'來檢查單元格是否爲空 –