Now we are really getting somewhere!
If I apply the formulas for conditional formatting that you gave
previously;
=AND(B4>=TODAY();B4<=DATE(YEAR(TODAY());MONTH(TODAY())+1;DAY(TODAY())))
and
=AND(B4>=TODAY();B4<=DATE(YEAR(TODAY());MONTH(TODAY())+3;DAY(TODAY())))
cells do indeed change formatting to colors I have chosen. And I can
fill in new similar looking dates in cells, like 20080530, without
Excel kicking back with #####.
But, when I do fill in a date such as I use, all conditional
formatting goes away! There is no longer any conditional formatting at
all left for that cell.
If I fill something other than a date, like "Bob", formatting remains
intact. So it seems that when your VBA code is triggered it also
erases the CF.
If I have a 'date' such as 20080707 in a cell, say B4, and construct a
new CF with your formulas then B4 will take on the format I want it
to. But if I rather use the "format painter" from another cell on B4
then it will lose its CF, and jump almost 2000 years into the future,
to 3963-07-06.
Time travel?
Thank you for taking the time!
Previously said:
Try this small adjustment
Private Sub Worksheet_Change(ByVal Target As Range)
Const WS_RANGE As String = "B2:B20"
On Error GoTo ws_exit
Application.EnableEvents = False
If Not Intersect(Target, Me.Range(WS_RANGE)) Is Nothing Then
With Target
If Not IsDate(.Value2) Then
.Value = DateSerial(Left$(.Value2, 4), Mid$(.Value2, 5, 2),
Right$(.Value2, 2))
End If
.NumberFormat = "yyyymmdd"
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, _
Formula1:="=AND(" & .Address(False, False)
& _
">=TODAY()," & .Address(False, False) & _
"<=DATE(YEAR(TODAY()),MONTH(TODAY())+1,DAY(TODAY())))"
.FormatConditions.Add Type:=xlExpression, _
Formula1:="=AND(" & .Address(False, False)
& _
">=TODAY()," & .Address(False, False) & _
"<=DATE(YEAR(TODAY()),MONTH(TODAY())+3,DAY(TODAY())))"
.FormatConditions(1).Interior.ColorIndex = 3
.FormatConditions(2).Interior.ColorIndex = 6
End With
End If
ws_exit:
Application.EnableEvents = True
End Sub
Lars
Stockholm