Private Sub btnEntry_Click()
On Error GoTo errorStep
Const PRODUCT_TBL_NAME As String = "商品マスタ"
Const PRODUCT_NAME_COLUMN As Long = 2 '商品名が登録されているカラム
Dim productTbl As Worksheet
Set productTbl = ThisWorkbook.Worksheets(PRODUCT_TBL_NAME)
With productTbl
Dim productRecord As Variant
productRecord = .Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, .Cells(1, .Columns.Count).End(xlToLeft).Column)).Value
End With
Dim existsItem As Boolean
existsItem = False
Dim i As Long
For i = LBound(productRecord, 1) + 1 To UBound(productRecord, 1) '1行目はタイトルなので+1にて検索を省く
If productRecord(i, PRODUCT_NAME_COLUMN) = txtGoods.Text Then
existsItem = True
GoTo errorStep
End If
Next
With productTbl
.Range(.Cells(UBound(productRecord, 1) + 1, 1), .Cells(UBound(productRecord, 1) + 1, PRODUCT_NAME_COLUMN)).Value = Array(UBound(productRecord, 1), txtGoods.Text)
End With
With txtGoods
.Value = ""
.SetFocus
End With
errorStep:
If existsItem Then MsgBox txtGoods.Text & "もとい、まぐろが重複しています", vbCritical + vbOKOnly, "重複"
ElseIf Err.Number <> 0 Then MsgBox Err.Number & Err.Description
End Sub