比较date和突出结果

我正在比较我工作表中D和E列的两个date。

D列包含源date,E列包含开始date。

我有4个案例来比较date。

案例1.如果巫师date小于开始date的2周,则按时打印项目。

情况2:如果源date大于4周的开始date,则打印项目延迟。

情况3:如果来源date在开始date的2到4周之间,则打印项目正在重新进行。

我已经为上述情况派生了代码,现在

案例4:不是每次列E都充满date,有时候是空的。

那么,我将如何在这里添加一个空声明。 我试图添加一个空声明,但失败了。

下面是代码。

Sub dateCompare() zLastRow = Range("D" & Rows.Count).End(xlUp).Row For r = 2 To zLastRow zWeeks = (Cells(r, "E") - Cells(r, "D")) / 7 Select Case zWeeks Case Is > 4 zcolour = vbRed Ztext = "Project Delayed " & Int(zWeeks) & " weeks" Case 2 To 4 zcolour = vbYellow Ztext = "Project Remaining" Case Is < 2 zcolour = vbGreen Ztext = "Project On-Time" Case Else zcolour = xlNone Ztext = " Check Status" End Select Cells(r, "F").Interior.Color = zcolour Cells(r, "F") = Ztext Next End Sub 

在计算zWeeks之前,检查列“E”不为空,用If Len(Trim(Cells(r, "E"))) = 0 Then 。 之后,使用您的Select Case

此外,可以使用DateDiff函数(第一个参数为"ww" (周))直接计算“E”列和“D”列中dat之间的差异。

 Option Explicit Sub dateCompare() Dim r As Long, zLastRow As Long Dim zWeeks As Double, zcolour As Long Dim Ztext As String zLastRow = Cells(Rows.Count, "D").End(xlUp).Row For r = 2 To zLastRow If Len(Trim(Cells(r, "E"))) = 0 Then ' column "E" is empty ' do something.... Else ' column "E" is not empty zWeeks = DateDiff("ww", Cells(r, "D"), Cells(r, "E")) Select Case zWeeks Case Is > 4 zcolour = vbRed Ztext = "Project Delayed " & Int(zWeeks) & " weeks" Case 2 To 4 zcolour = vbYellow Ztext = "Project Remaining" Case Is < 2 zcolour = vbGreen Ztext = "Project On-Time" Case Else zcolour = xlNone Ztext = " Check Status" End Select Cells(r, "F").Interior.Color = zcolour Cells(r, "F") = Ztext End If Next r End Sub