I am trying to compare a column filled with dates with the current date. If a date in the column is smaller than current date then i paint the whole row in red. My code is below:
With wb8.Sheets(1)
For Each rCell In .Range(.Cells(2, "W"), .Cells(.Rows.Count, "W").End(xlUp))
If IsDate(rCell) Then
If Format(rCell, "dd.mm.yyyy") < Format(Date, "dd.mm.yyyy") Then
rCell.EntireRow.Interior.Color = RGB(255, 0, 0)
End If
End If
Next rCell
End With
Now this code works but the results are not accurate as it is seen in the image below
I suspect that the comparison is not working properly but i am not that skilled in VBA so can't find the problem. Could someone help me out please?
CodePudding user response:
From MSDN:
When you enter a date, the date is converted into a serial number that represents the number of elapsed days starting with 1 for January 1, 1900. For example, if you enter July 5, 1998, Excel converts the date to the serial number 35981.
This means that you can simply compare the serial numbers as integers.
If rCell.Value < Date Then
rCell.EntireRow.Interior.Color = RGB(255, 0, 0)
End If