1

ユーザーが指定した日付よりも前の日付を特定の列で検索しようとしているだけです。

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

私の問題は、これが常に正しいものを選択するとは限らないことです。2 桁の月または日を使用すると、1 桁の月と日は完全に無視されます。セルは既に 03/14/01 の日付形式でフォーマットされているため、正常に表示されますが、値が一致しません。値に一致するように表示を変更することはできますか? もしそうなら、どうすればいいですか?

前もって感謝します。

更新: ケビンの助けを借りて、これを解決することができました。誰かが役に立つと思った場合に備えて、完成したコードは次のとおりです。

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
4

1 に答える 1

1

TheAnswer文字列として定義しましたrCell.Valueが、 は日付になるため、結果に一貫性がなくなります。これを試して:

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) を使用するのではなく、設定範囲または動的範囲を使用することを検討してください。

于 2012-11-15T18:54:03.903 に答える