Forum Discussion

Fveeckma's avatar
Fveeckma
Copper Contributor
Oct 26, 2023

Conditional formatting when using VBA

Goodday, 

I used following code to automatically generate a timestamp in a given cell:

 

"Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Handler
If Target.Column = 1 And Target.Value <> "" Then
Application.EnableEvents = False
Target.Offset(0, 1) = Format(Date, "dd/mm/yyyy")
Application.EnableEvents = True
End If
Handler:
End Sub"

 

After that i wanted to use conditional formatting on the results (dates) i was given. In this case i wanted to colour code the dates RED in case they were smaller than Today()+7.

I Guess it has something  to do with the fact that the resulting 'dates' i get from the VBA code isn't really recognized as a 'date' but i don't know how to work around this.

Any help is much appreciated.

Thanks in advance!
Kind regards,

Flor

 

  • Fveeckma 

    Change the line

     

    Target.Offset(0, 1) = Format(Date, "dd/mm/yyyy")

     

    to

     

    Target.Offset(0, 1) = Date

     

    Apply the desired date format to column B (you have to do that only once)

Resources