Hi Lucas,

The "Chr(13) & Chr(7)" combo is used to determine if the cell is blank. Word inserts these non-printing characters in each table's cell by default. These characters tripped me up for a while until Tommy told me about them.


I modified my sample for you to detect when the DueDate is withing 5 days of RightNow. There are two reasons why the code you posted won't work. The first is syntax - you need to remove the first " and the &, as well as the quote around RightNow (RightNow is a variable - by placing quotes around it your make it a literal string). So it should have initially read like:
[vba]
ElseIf DueDate >= DateAdd("d", 5, RightNow) Then
[/vba]

If you had written the statement like that, you then would have a logic error because the statement would have highlighted any rows >= 5 days in the future, not just rows within 5 days. You need an AND statement to test for that condition.

Also, since this is not an exact copy of the statement I actually used, you may need some further logical testing. For instance, if there is a Date Complete cell, you would want to make sure it isn't blank when you test the DueDate. Otherwise, you could end up marking an item RED when it is actually complete. Just modify the same AND statement in the sample code to suit your situation.

[vba]
Sub HighLightColumnOfCells()
Dim DueDate, RightNow As Date

RightNow = Format(Now, "mm/dd/yyyy")

For I = 1 To ActiveDocument.Tables(1).Rows.Count
DueDate = Format(Left$(ActiveDocument.Tables(1).Cell(I, 5).Range.Text, Len(ActiveDocument.Tables(1).Cell(I, 5).Range.Text) - 1), "mm/dd/yyyy")

If ActiveDocument.Tables(1).Cell(I, 5).Range.Text = Chr(13) & Chr(7) Then
'Cell is blank, color it white
ActiveDocument.Tables(1).Cell(I, 5).Shading.BackgroundPatternColor = wdColorWhite

ElseIf DateDiff("d", RightNow, DueDate) >= 1 And DateDiff("d", RightNow, DueDate) <= 5 Then
'DueDate is within 5 days, color it yellow
ActiveDocument.Tables(1).Cell(I, 5).Shading.BackgroundPatternColor = wdColorYellow

ElseIf DueDate <= RightNow Then
'DueDate is today, color it red
ActiveDocument.Tables(1).Cell(I, 5).Shading.BackgroundPatternColor = wdColorRed

ElseIf DateDiff("d", RightNow, DueDate) > 5 Then
'DueDate is more than 5 days in the future, color it Green
ActiveDocument.Tables(1).Cell(I, 5).Shading.BackgroundPatternColor = wdColorGreen
End If
Next I

End Sub
[/vba]

Cheers,
James