Excel VBA | VBA 日付・時刻関数一覧

Excel VBA VBA
スポンサーリンク

こちらに「日付時刻チェックや納期管理の実務サンプル集」をまとめました
IsDateDateDiffDateAdd などを組み合わせることで、入力チェックから納期管理まで自動化できます。


実務サンプル集

1. 日付入力チェック(IsDate)

Sub CheckDateInput()
    Dim val As Variant
    val = Range("A2").Value
    
    If IsDate(val) Then
        MsgBox "有効な日付です → " & CDate(val)
    Else
        MsgBox "日付形式が不正です"
    End If
End Sub
VB

👉 誤った日付入力(例:2025/13/1)を検出


2. 納期までの日数計算(DateDiff)

Sub CheckDueDays()
    Dim dueDate As Date
    dueDate = Range("B2").Value
    
    If IsDate(dueDate) Then
        MsgBox "納期まで残り " & DateDiff("d", Date, dueDate) & " 日です"
    Else
        MsgBox "納期日が不正です"
    End If
End Sub
VB

👉 今日から納期までの日数を自動計算


3. 納期超過チェック(DateDiff)

Sub CheckOverdue()
    Dim dueDate As Date
    dueDate = Range("C2").Value
    
    If IsDate(dueDate) Then
        If DateDiff("d", dueDate, Date) > 0 Then
            MsgBox "納期超過! " & DateDiff("d", dueDate, Date) & " 日遅れています"
        Else
            MsgBox "納期内です"
        End If
    End If
End Sub
VB

👉 納期を過ぎているかどうかを判定


4. 納期アラート(残り日数が閾値以下なら警告)

Sub DueAlert()
    Dim dueDate As Date
    Dim remain As Long
    
    dueDate = Range("D2").Value
    If IsDate(dueDate) Then
        remain = DateDiff("d", Date, dueDate)
        If remain <= 3 And remain >= 0 Then
            MsgBox "⚠ 納期まで残り " & remain & " 日です。要注意!"
        End If
    End If
End Sub
VB

👉 納期が迫った案件を自動で警告


5. 月末納期の自動設定(DateAdd)

Sub SetMonthEndDue()
    Dim targetDate As Date
    targetDate = Date
    
    ' 今月末を納期に設定
    Range("E2").Value = DateAdd("m", 1, DateSerial(Year(targetDate), Month(targetDate), 0))
End Sub
VB

👉 月末を自動計算して納期セルに設定


6. 納期一覧を走査して色付け(期限管理)

Sub HighlightDueDates()
    Dim ws As Worksheet, lastRow As Long, i As Long, dueDate As Date, remain As Long
    Set ws = Sheets("納期管理")
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    For i = 2 To lastRow
        If IsDate(ws.Cells(i, 2).Value) Then
            dueDate = ws.Cells(i, 2).Value
            remain = DateDiff("d", Date, dueDate)
            
            If remain < 0 Then
                ws.Cells(i, 2).Interior.Color = RGB(255, 150, 150) ' 赤:期限超過
            ElseIf remain <= 3 Then
                ws.Cells(i, 2).Interior.Color = RGB(255, 255, 150) ' 黄:期限迫る
            Else
                ws.Cells(i, 2).Interior.Color = RGB(200, 255, 200) ' 緑:余裕あり
            End If
        End If
    Next i
End Sub
VB

👉 納期一覧を自動チェックし、期限状況を色分け


✅ まとめ

  • IsDate → 入力が正しい日付かどうかを判定
  • DateDiff → 納期までの日数や超過日数を計算
  • DateAdd → 月末や翌週などの納期を自動設定
  • 自動色付けやアラートで、納期管理を効率化

💡 この仕組みを応用すれば、「納期一覧を集計してグラフ化」「担当者別に納期アラートをメール送信」といった高度な管理も可能です。

VBA
スポンサーリンク
シェアする
@lifehackerをフォローする
スポンサーリンク
タイトルとURLをコピーしました