0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Excel VBAによる日付セルの背景色変更の方法(期日に近いセルを黄色に・期日当日および過ぎたセルを赤色に)

Last updated at Posted at 2022-09-07

既存のマクロに処理式を追加することになり、備忘録として書き残します。
読みづらいとは思いますが、参考にしていただければ幸いです。

処理の内容は、
①本日が納期の5日前から1日前の場合、AF列「納期」欄のセルを黄色に塗りつぶす。
②本日が納期当日および納期以降の場合、AF列「納期」欄のセルを赤色に塗りつぶす。
③AI列「ステータス」欄が「完了」の場合、塗りつぶしなし。

Option Explicit

Sub CellsColor()
   
    Dim i As Integer
    Dim A As Date
    Dim B As Integer
    Dim C As String
   
    'AF列「納期」欄の記載開始行を設定。
    i = 6
   
    '本日の日付を取得する。
    A = Date
   
    'AF列が空白になるまで処理を繰り返す。
    Do Until Cells(i, 32).Value = ""
   
        '納期までの日数をBとする。
        B = Cells(i, 32).Value - A
               
        'AI列「ステータス」欄の値をCとする。
        C = Cells(i, 35).Value
   
        '納期より5日前以下になった場合、AF列のセルを黄色に塗りつぶす。
        '納期当日および納期以降になった場合、AF列のセルを赤色に塗りつぶす。
        'AI列が「完了」の場合、塗りつぶしなし。
        If B <= 5 And B > 0 And C <> "完了" Then
            Cells(i, 32).Interior.Color = RGB(255, 255, 0) '背景色:黄色
        ElseIf B <= 0 And C <> "完了" Then
            Cells(i, 32).Interior.Color = RGB(255, 0, 0) ' 背景色:赤色
        Else
            Cells(i, 32).Interior.Color = xlNone '塗りつぶしなし
        End If
   
        i = i + 1
    Loop
   
End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?