在Excel中使用VBA我有一個代碼,比較輸入的日期和當前的日期,並根據結果系統將以正確的顏色填充單元格。比較日期之間,以填補適當的顏色
其中代碼比較在四個條件。 如果輸入的日期減去的電流爲:
- = 0
- 小於0
- 4和10
使用IF語句,但在間 1和4之間
代碼:
Private Sub CommandButton1_Click()
Dim i As Integer
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 Exit Sub
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 = vbBlue
ElseIf (VBA.CDate(Cells(i, 3)) - VBA.Date()) > 4 And (VBA.CDate(Cells(i, 3)) - VBA.Date()) < 10 Then
Cells(i, 3).Interior.Color = vbRed
End If
Next
End Sub
爲什麼要使用VBA?條件格式可以做這種事情。無論如何,如果你有錯誤 - 請更具體。什麼行會拋出錯誤?什麼錯誤信息? –
我試圖使用條件格式化,但它沒有工作,因爲我想 –