如何searchdate早于特定date的单元格的列并突出显示它们?

我只是试图search特定列的任何date早于用户指定。

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 

我的问题是,这并不总是select正确的。 如果我使用两位数的月份或date,则完全忽略了一位数字的月份和date。 我已经使用03/14/01date格式格式化单元格,所以它们显示正常,但是值不匹配。 我可以简单地改变什么显示匹配的价值? 如果是这样,我该怎么做?

提前致谢。

更新:在凯文的帮助下,我能够解决这个问题。 如果有人发现它有用,这是我最终的代码:

 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 

您已将TheAnswer定义为一个string,而rCell.Value将是一个date,因此结果将不一致。 尝试这个:

 Dim rCell As Range Dim TheAnswer$ Dim ConvertedDate# 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") ConvertedDate = CDate(TheAnswer) For Each rCell In ActiveSheet.Range("D:D").cells If rCell.Value < ConvertedDate Then rCell.Interior.Color = RGB(255, 102, 0) End If Next rCell 

另外,考虑不使用整个列(D:D),而是使用设置范围或dynamic范围。