Excel VBA | セル全体を対象にした入力チェックマクロ集

VBA
スポンサーリンク

セル全体を対象にした入力チェックマクロ集をまとめました。
未入力・数値・日付・文字数など、よくあるチェックを網羅しています。


入力チェックマクロ集

1. 未入力チェック(必須項目)

Sub CheckMissing()
    Dim ws As Worksheet, lastRow As Long, i As Long
    Set ws = Sheets("入力シート")
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    For i = 2 To lastRow
        If Trim(ws.Cells(i, 1).Value) = "" Then
            ws.Cells(i, 1).Interior.Color = RGB(255, 200, 200) ' 赤で警告
        End If
    Next i
End Sub
VB

👉 必須項目が空欄なら赤色で強調


2. 数値チェック(IsNumeric)

Sub CheckNumeric()
    Dim ws As Worksheet, lastRow As Long, i As Long
    Set ws = Sheets("入力シート")
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    
    For i = 2 To lastRow
        If Not IsNumeric(ws.Cells(i, 2).Value) Then
            ws.Cells(i, 2).Interior.Color = RGB(255, 255, 150) ' 黄色で警告
        End If
    Next i
End Sub
VB

👉 数値以外が入力されていたら黄色で警告


3. 日付チェック(IsDate)

Sub CheckDate()
    Dim ws As Worksheet, lastRow As Long, i As Long
    Set ws = Sheets("入力シート")
    lastRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
    
    For i = 2 To lastRow
        If Not IsDate(ws.Cells(i, 3).Value) Then
            ws.Cells(i, 3).Interior.Color = RGB(173, 216, 230) ' 水色で警告
        End If
    Next i
End Sub
VB

👉 日付以外の入力を検出


4. 文字数チェック(Len関数)

Sub CheckLength()
    Dim ws As Worksheet, lastRow As Long, i As Long
    Set ws = Sheets("入力シート")
    lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
    
    For i = 2 To lastRow
        If Len(ws.Cells(i, 4).Value) > 10 Then
            ws.Cells(i, 4).Interior.Color = RGB(255, 182, 193) ' ピンクで警告
        End If
    Next i
End Sub
VB

👉 文字数制限(例:10文字以内)を超えたら警告


5. 総合チェック(複数条件まとめ)

Sub CheckAll()
    Dim ws As Worksheet, lastRow As Long, i As Long
    Set ws = Sheets("入力シート")
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ' 色リセット
    ws.Range("A2:D" & lastRow).Interior.ColorIndex = xlNone
    
    For i = 2 To lastRow
        ' A列:必須
        If Trim(ws.Cells(i, 1).Value) = "" Then ws.Cells(i, 1).Interior.Color = RGB(255, 200, 200)
        ' B列:数値
        If Not IsNumeric(ws.Cells(i, 2).Value) Then ws.Cells(i, 2).Interior.Color = RGB(255, 255, 150)
        ' C列:日付
        If Not IsDate(ws.Cells(i, 3).Value) Then ws.Cells(i, 3).Interior.Color = RGB(173, 216, 230)
        ' D列:文字数制限
        If Len(ws.Cells(i, 4).Value) > 10 Then ws.Cells(i, 4).Interior.Color = RGB(255, 182, 193)
    Next i
    
    MsgBox "入力チェック完了"
End Sub
VB

👉 1つのマクロで未入力・数値・日付・文字数をまとめてチェック


✅ まとめ

  • IsNumeric / IsDate / Len / Trim を組み合わせると強力な入力チェックが可能
  • Interior.Color でエラー箇所を色付け → 視覚的にわかりやすい
  • 総合チェックマクロで一括検証すれば、提出前の検証ツールとして実務で即活用できる

💡 この仕組みを応用すれば、エラー一覧を別シートに出力したり、エラー件数を集計することも可能です。

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