Excel VBA 逆引き集 | 保存前チェック

Excel VBA
スポンサーリンク

保存前チェックの基本

Excelで「保存する前に入力内容を確認したい」「必須項目が空欄なら保存させない」といった場面では 保存前チェック を組み込むと便利です。
初心者向けに、コード例やテンプレートをかみ砕いて説明します。


基本の考え方

  • 保存前チェックとは?
    データを保存する直前に「必須項目が入力されているか」「数値が正しいか」などを確認する仕組み。
  • 用途:
    • 入力漏れ防止
    • 不正データの保存防止
    • 保存前に確認メッセージを出す

テンプレ1:必須項目の空欄チェック

UserFormに TextBox1(氏名)TextBox2(年齢)CommandButton1(保存ボタン) を配置。

Private Sub CommandButton1_Click()
    ' 氏名が空欄なら保存させない
    If TextBox1.Value = "" Then
        MsgBox "氏名を入力してください"
        Exit Sub
    End If
    
    ' 年齢が空欄なら保存させない
    If TextBox2.Value = "" Then
        MsgBox "年齢を入力してください"
        Exit Sub
    End If
    
    ' 保存処理(例:シートに書き込み)
    Dim ws As Worksheet: Set ws = Worksheets("Data")
    Dim lastRow As Long: lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ws.Cells(lastRow + 1, 1).Value = TextBox1.Value
    ws.Cells(lastRow + 1, 2).Value = TextBox2.Value
    
    MsgBox "保存しました!"
End Sub
VB
  • ポイント:
    • Exit Sub で保存処理を中断。
    • 必須項目が空欄なら警告を出す。

テンプレ2:数値チェック(年齢は数字のみ)

Private Sub CommandButton1_Click()
    If Not IsNumeric(TextBox2.Value) Then
        MsgBox "年齢は数値で入力してください"
        Exit Sub
    End If
    
    ' 保存処理
    Dim ws As Worksheet: Set ws = Worksheets("Data")
    Dim lastRow As Long: lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ws.Cells(lastRow + 1, 1).Value = TextBox1.Value
    ws.Cells(lastRow + 1, 2).Value = TextBox2.Value
    
    MsgBox "保存しました!"
End Sub
VB
  • ポイント:
    • IsNumeric で数値チェック。
    • 不正な入力を防げる。

テンプレ3:保存前に確認メッセージを出す

Private Sub CommandButton1_Click()
    Dim ans As VbMsgBoxResult
    ans = MsgBox("この内容で保存しますか?", vbYesNo)
    
    If ans = vbNo Then
        MsgBox "保存をキャンセルしました"
        Exit Sub
    End If
    
    ' 保存処理
    Dim ws As Worksheet: Set ws = Worksheets("Data")
    Dim lastRow As Long: lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ws.Cells(lastRow + 1, 1).Value = TextBox1.Value
    ws.Cells(lastRow + 1, 2).Value = TextBox2.Value
    
    MsgBox "保存しました!"
End Sub
VB
  • ポイント:
    • MsgBox で Yes/No を選ばせる。
    • ユーザーが確認してから保存できる。

テンプレ4:複数項目をまとめてチェック

Private Sub CommandButton1_Click()
    Dim errorMsg As String
    
    If TextBox1.Value = "" Then errorMsg = errorMsg & "氏名が未入力です" & vbCrLf
    If TextBox2.Value = "" Then errorMsg = errorMsg & "年齢が未入力です" & vbCrLf
    If Not IsNumeric(TextBox2.Value) And TextBox2.Value <> "" Then errorMsg = errorMsg & "年齢は数値で入力してください" & vbCrLf
    
    If errorMsg <> "" Then
        MsgBox errorMsg
        Exit Sub
    End If
    
    ' 保存処理
    Dim ws As Worksheet: Set ws = Worksheets("Data")
    Dim lastRow As Long: lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    
    ws.Cells(lastRow + 1, 1).Value = TextBox1.Value
    ws.Cells(lastRow + 1, 2).Value = TextBox2.Value
    
    MsgBox "保存しました!"
End Sub
VB
  • ポイント:
    • 複数のエラーをまとめて表示。
    • ユーザーが一度に修正できる。

例題で練習

'例1:必須項目の空欄チェック
'例2:数値チェック
'例3:保存前に確認メッセージ
'例4:複数項目をまとめてチェック
VB

初心者向けポイント

  • 保存処理の前に必ずチェックを入れるExit Sub で中断できる。
  • IsNumericで数値判定 → 数値入力欄に便利。
  • MsgBoxで確認を取る → 誤保存を防げる。
  • 複数エラーをまとめると親切 → ユーザーが一度に修正できる。

👉 この「保存前チェックテンプレ」を覚えておけば、Excel VBAで 入力漏れや誤入力を防ぎ、安心してデータを保存できるフォーム を簡単に作成できます。

タイトルとURLをコピーしました