I’m simply trying to search a specific column for any dates earlier than what the user specifies.
Dim rCell As Range
Dim TheAnswer$
TheAnswer = InputBox("In M/D/YYYY format, enter the first day of the month for which this report is being run." & _
vbCr & vbCr & "For example, you would enter ""12/1/2012"" for the December 2012 report.", "Enter Date M/D/YYYY")
For Each rCell In ActiveSheet.Range("D:D").Cells
If rCell.Value < TheAnswer Then
rCell.Interior.Color = RGB(255, 102, 0)
End If
Next rCell
My problem is that this doesn’t always pick the right ones. If I use a month or day with two digits, it completely ignores those months and days with one digit. I already formatted the cells with the 03/14/01 date format, so they display fine, but the value doesn’t match. Could I simply change what displays to match the value? If so, how do I do that?
Thanks in advance.
Update: With help from Kevin, I was able to solve this. In case anyone finds it useful, here’s my finalized code:
Range(Cells(2, 4), Cells(Rows.Count, 4).End(xlUp)).Select
Dim rCell As Range
Dim TheAnswer$
Dim ConvertedDate#
TheAnswer = InputBox("In M/D/YY format, enter the first day of the month for which this report is being run." & _
vbCr & vbCr & "For example, you would enter ""12/1/2012"" for the December 2012 report.", "Enter Date M/D/YY")
ConvertedDate = CDate(TheAnswer)
For Each rCell In Selection
If rCell.Value <> "" Then
If rCell.Value < ConvertedDate Then
rCell.Interior.Color = RGB(255, 102, 0)
End If
End If
Next rCell
You’ve defined
TheAnsweras a string, whereas therCell.Valuewill be a date, so the results will be inconsistent. Try this:Also, consider not using the entire column (D:D) and instead use a set range or a dynamic range.