unote 書けば書くほどに

barcode

Sub InputCode()

Dim InputCode As Variant
Dim lastRow As Long
Dim inputName As Variant
Dim inputQuantity As Variant
Dim recordSheet As Worksheet
Dim listSheet As Worksheet
Dim i As Long

' 記録シートを選択
Set recordSheet = Worksheets("記録")

' 数量を入力してくださいのメッセージを表示
InputCode = InputBox("コードを入力してください")

' 入力された値を記録シートのC列の最終行に追加
lastRow = recordSheet.Cells(Rows.Count, "A").End(xlUp).Row

If InputCode = "" Then
MsgBox "キャンセルしました"
Exit Sub ' すでに一覧に存在する場合は処理を終了
Else
recordSheet.Range("A" & lastRow + 1).Value = InputCode
End If

' 数量を入力してくださいのメッセージを表示
inputQuantity = InputBox("数量を入力してください")

If inputQuantity = "" Then
recordSheet.Range("A" & lastRow + 1).Value = ""
MsgBox "キャンセルしました"
Exit Sub ' すでに一覧に存在する場合は処理を終了
Else
' 入力された値を記録シートのC列の最終行に追加
lastRow = recordSheet.Cells(Rows.Count, "C").End(xlUp).Row
recordSheet.Range("C" & lastRow + 1).Value = inputQuantity
recordSheet.Range("B" & lastRow + 1).Value = Now
End If


' 一覧シートを参照
Set listSheet = ThisWorkbook.Sheets("一覧")

' 入力された値が一覧に存在しない場合、一覧に追加
inputName = recordSheet.Range("A" & lastRow + 1).Value
For i = 1 To listSheet.Cells(Rows.Count, "A").End(xlUp).Row
If listSheet.Range("A" & i).Value = inputName Then
Exit Sub ' すでに一覧に存在する場合は処理を終了
End If
Next i
listSheet.Range("A" & listSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1).Value = inputName

' 品名を入力してくださいのメッセージを表示
inputName = InputBox("品名を入力してください")

' 入力された品名を一覧に追加
listSheet.Range("B" & listSheet.Cells(Rows.Count, "B").End(xlUp).Row + 1).Value = inputName

' 予定数を入力してくださいのメッセージを表示
inputQuantity = InputBox("予定数を入力してください")

' 入力された予定数を一覧に追加
listSheet.Range("C" & listSheet.Cells(Rows.Count, "C").End(xlUp).Row + 1).Value = inputQuantity

Range("A" & lastRow + 1).EntireRow.Select

MsgBox "完了しました"

End Sub