2017-06-14 206 views
1

我正在比较两列(D和E)中的两个日期。 D列中的日期是源日期,E列中的日期是项目的开始日期。比较两列中的日期并突出显示单元格

我正在计算两个日期的差异,并将结果粘贴到列F中并相应地突出显示。

我有4例我:

  • 案例1:如果采购日期>4周开始日期则状态是“工程延误”
  • 案例2:如果源日期是< 2周开始日期的,则状态为“时间工程”。案例3:如果源日期为< 4周,开始日期> 2周,状态为“项目剩余”。

我已经实现了树的情况。

  • 案例4:在某些情况下,列E可能没有任何日期并且为空。在这种情况下,我想有一个if case,说“项目未启动”。

我试过它为空,但我无法弄清楚,为什么这种情况下4不工作。

Sub dateCompare() 
    zLastRow = Range("D" & Rows.Count).End(xlUp).Row 'last data row 

    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    For r = 2 To zLastRow 
     zWeeks = (Cells(r, "E") - Cells(r, "D"))/7 'date difference in weeks 

     Select Case zWeeks 
      Case Is > 4        'later than 4 weeks 
       zColour = vbRed 
       zText = "Project delayed " & Int(zWeeks) & " weeks" 

      Case 2 To 4        'between 2 and 4 weeks 
       zColour = vbYellow 
       zText = "Project ongoing" 

      Case Is < 2        'less than 2 weeks 
       zColour = vbGreen 
       zText = "Project On-Time" 

      Case Else         'in case of duff data.. 
       zColour = xlNone 
       zText = " check dates" 
     End Select 

     Cells(r, "D").Interior.Color = zColour   'set cell background colour 
     Cells(r, "F") = zText       'set project status 
    Next 
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
End Sub 

请帮我解决这个问题。
问候, Mikz

+0

为什么不能简单地增加一个'if' statment之前'zWeeks =(细胞(R, “E”) - 细胞(R, “d”))/7'?类似于“如果单元格(r,”E“)=”“则zText =”项目未启动“其他RestOfYourCode”。 – CMArg

回答

0

检查:

Sub dateCompare() 
zLastRow = Range("D" & Rows.Count).End(xlUp).Row 'last data row 

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
For r = 2 To zLastRow 
    If IsEmpty(Cells(r, "E").Value) Then 'check if column is empty 
     zColour = xlNone 
     zText = " check dates" 
    else 
     zWeeks = (Cells(r, "E") - Cells(r, "D"))/7  'date difference in weeks 

     Select Case zWeeks 
      Case Is > 4           'later than 4 weeks 
       zColour = vbRed 
       zText = "Project delayed " & Int(zWeeks) & " weeks" 
      Case 2 To 4           'between 2 and 4 weeks 
       zColour = vbYellow 
       zText = "Project ongoing" 
      Case Is < 2           'less than 2 weeks 
       zColour = vbGreen 
       zText = "Project On-Time" 
     End Select 
    End if 

    Cells(r, "D").Interior.Color = zColour    'set cell background colour 
    Cells(r, "F") = zText        'set project status 

Next 
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

End Sub 
+0

@Khual Rohilla做了代码帮助?反馈总是受欢迎的。 – CMArg

相关问题