Option Explicit
Const mode_add As Long = 1 '登録
Const mode_chg As Long = 2 '変更
Const mode_del As Long = 3 '削除
Const mode_find As Long = 4 '検索
Dim ws As Worksheet '接点記録シート
Dim LastRow As Long '最終行
Dim execRow As Long '実行行
Dim execMode As Long '実行モード
Private Sub UserForm_Initialize()
Me.ComboBox1.AddItem ("comb1")
Me.ComboBox1.AddItem ("comb2")
Me.ComboBox1.AddItem ("comb3")
Me.ComboBox1.AddItem ("comb4")
Set ws = Worksheets("接点記録")
Call disable_exec_Button
End Sub
Private Sub CommandButton登録_Click()
Call find_number(mode_add)
End Sub
Private Sub CommandButton変更_Click()
Call find_number(mode_chg)
End Sub
Private Sub CommandButton削除_Click()
Call find_number(mode_del)
End Sub
Private Sub CommandButton検索_Click()
Call find_number(mode_find)
End Sub
'登録、変更、削除、検索共通処理
Private Sub find_number(ByVal mode As Long)
Dim re As Object
Dim wrow As Long
Dim flag As Boolean
Dim i As Long
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "^\d{8}$"
If re.test(Me.TextBox1.Text) = False Then
MsgBox ("入力エラー")
Exit Sub
End If
execMode = mode
'最終行検出
flag = False
LastRow = ws.Cells(rows.Count, "B").End(xlUp).Row
For wrow = 2 To LastRow
If Me.TextBox1.Text = ws.Cells(wrow, "B").Value Then
flag = True
execRow = wrow
Exit For
End If
Next
'該当番号なしのケース
If flag = False Then
If execMode <> mode_add Then
MsgBox ("該当番号なし")
Exit Sub
End If
'登録処理
'データ部クリア
Call clear_data
Call disable_menu_Button
Call enable_hozon_Button
Exit Sub
End If
'該当番号ありのケース
If execMode = mode_add Then
MsgBox ("該当番号登録済み")
Exit Sub
End If
'テキストボックスへ表示
Call load_data(wrow)
'検索の場合、終了
If execMode = mode_find Then Exit Sub
'変更の場合
If execMode = mode_chg Then
Call disable_menu_Button
Call enable_hozon_Button
End If
'削除の場合
If execMode = mode_del Then
Call disable_menu_Button
Call enable_delete_Button
End If
End Sub
Private Sub CommandButton保存_Click()
Dim wrow As Long
If execMode = mode_add Then
wrow = LastRow + 1
Else
wrow = execRow
End If
ws.Cells(wrow, "B").Value = Me.TextBox1.Text '番号
ws.Cells(wrow, "C").Value = Me.TextBox2.Text '氏名
ws.Cells(wrow, "D").Value = Me.TextBox3.Text '住所
ws.Cells(wrow, "E").Value = Me.TextBox4.Text '訪問日
ws.Cells(wrow, "F").Value = Me.ComboBox1.Text '見積
ws.Cells(wrow, "G").Value = Me.TextBox5.Text '担当者
ws.Cells(wrow, "H").Value = Me.TextBox6.Text '内容
ws.Cells(wrow, "I").Value = Me.TextBox7.Text '成約日
ws.Cells(wrow, "J").Value = Me.TextBox8.Text '金額
ws.Cells(wrow, "K").Value = Me.TextBox9.Text '備考
MsgBox ("保存完了")
Call clear_all_data '全項目をクリア
Call enable_menu_Button
Call disable_exec_Button
End Sub
Private Sub CommandButton削除実行_Click()
ws.rows(execRow).Delete
MsgBox ("削除完了")
Call clear_all_data '全項目をクリア
Call enable_menu_Button
Call disable_exec_Button
End Sub
Private Sub CommandButton中止_Click()
Call clear_all_data '全項目をクリア
Call enable_menu_Button
Call disable_exec_Button
End Sub
Private Sub CommandButton閉じる_Click()
Unload Me
End Sub
'全項目クリア
Private Sub clear_all_data()
Me.TextBox1.Value = ""
Call clear_data
End Sub
'データ部クリア
Private Sub clear_data()
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.TextBox4.Value = ""
Me.ComboBox1.Value = ""
Me.TextBox5.Value = ""
Me.TextBox6.Value = ""
Me.TextBox7.Value = ""
Me.TextBox8.Value = ""
Me.TextBox9.Value = ""
End Sub
'データ部設定
Private Sub load_data(ByVal wrow As Long)
Me.TextBox2.Value = ws.Cells(wrow, "C").Value '氏名
Me.TextBox3.Value = ws.Cells(wrow, "D").Value '住所
Me.TextBox4.Value = ws.Cells(wrow, "E").Value '訪問日
Me.ComboBox1.Value = ws.Cells(wrow, "F").Value '見積
Me.TextBox5.Value = ws.Cells(wrow, "G").Value '担当者
Me.TextBox6.Value = ws.Cells(wrow, "H").Value '内容
Me.TextBox7.Value = ws.Cells(wrow, "I").Value '成約日
Me.TextBox8.Value = ws.Cells(wrow, "J").Value '金額
Me.TextBox9.Value = ws.Cells(wrow, "K").Value '備考
End Sub
Private Sub disable_menu_Button()
Me.CommandButton登録.Enabled = False
Me.CommandButton変更.Enabled = False
Me.CommandButton削除.Enabled = False
Me.CommandButton検索.Enabled = False
Me.TextBox1.Enabled = False
End Sub
Private Sub enable_menu_Button()
Me.CommandButton登録.Enabled = True
Me.CommandButton変更.Enabled = True
Me.CommandButton削除.Enabled = True
Me.CommandButton検索.Enabled = True
Me.TextBox1.Enabled = True
End Sub
Private Sub disable_exec_Button()
Me.CommandButton保存.Enabled = False
Me.CommandButton中止.Enabled = False
Me.CommandButton削除実行.Enabled = False
End Sub
Private Sub enable_hozon_Button()
Me.CommandButton保存.Enabled = True
Me.CommandButton中止.Enabled = True
End Sub
Private Sub enable_delete_Button()
Me.CommandButton中止.Enabled = True
Me.CommandButton削除実行.Enabled = True
End Sub