I'm comparing the two dates in the two columns (D and E). The Dates in column D are source dates and the dates in column E are start date of the project.
I'm calculating the difference in two dates as weeks and pasting the result in the column F and highlighting it accordingly.
I have 4 cases with me:
- Case 1: If the sourcing date is > 4 weeks of start date then the status is "Project delayed".
- Case 2: If the source date is < 2 weeks of the start date then the status is "Project on time".
- Case 3: If the source date is < 4 weeks, > 2 weeks of the start date the status is "Project remaining".
I have achieved the tree cases.
- Case 4: there is a possibility that in some cases the column E does not have any date and it is empty. In this Situation, I would like to have an if case, that says "Project not started".
I tried it as Null but, I could not figure out, why this case 4 was not working.
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
Kindly help me to solve this issue.
Regards,
Mikz
ifstatment beforezWeeks = (Cells(r, "E") - Cells(r, "D")) / 7? Something likeif Cells(r, "E")="" then zText ="Project not started" else RestOfYourCode.