重复条目的预防措施 VBA Excel
Preventive measures for duplicate entries VBA Excel
我有一个用户表单,可以将新项目添加到我的库存中 sheet。
如果我可以让我的产品像主键一样,我想寻求帮助。这样 "product key" 就不能重复了。任何帮助将不胜感激。
我的代码在 link 中如下(似乎无法将我的代码粘贴到此框中,因为我一直收到错误消息,说我的代码格式不正确)抱歉不便之处:http://imgur.com/7xFDawO
Option Explicit
Private Sub cmdAdd_Click()
Dim row As Long
Do
row = row + 1
Loop Until (Sheets("Inventory").Cells(row, 1) = "")
Sheets("Inventory").Select
Sheets("Inventory").Cells(row, 1) = txtProdCodeAI.Value
Sheets("Inventory").Cells(row, 2) = txtProdNameAI.Value
Sheets("Inventory").Cells(row, 3) = txtQuantityAI.Value
Sheets("Inventory").Cells(row, 4) = txtSupplierAI.Value
Sheets("Inventory").Cells(row, 5) = txtSupplierNumberAI.Value
End Sub
Private Sub cmdCancel_Click()
Unload AddInventory
End Sub
P.S代码已添加到link中的图片描述中,但格式不正确
**
- 编辑
**
用户通过文本框输入产品代码、产品名称、数量、供应商(名称)和供应商联系电话。
主键位于 A 列并从第 2 行开始,因为有一个 header。
sheet 名字叫 "Inventory"。
如果输入相同的主键,将弹出错误消息"vbokonly - Error! Product code already exists. Kindly use Update feature."
只需检查 A 列中的值:
txtProdCodeAI.Value
像这样:
Dim keyExists as Boolean
keyExists = Not Range("A1:A" & row).Find(txtProdCodeAI.Value) Is Nothing
If keyExists Then
MsgBox txtProdCodeAI.Value & " already exists!", vbCritical
Exit Sub
End If
像这样把它放在你的代码中:
Private Sub cmdAdd_Click()
Dim row As Long
Dim keyExists as Boolean
Do
row = row + 1
Loop Until (Sheets("Inventory").Cells(row, 1) = "")
keyExists = Not Sheets("Inventory").Range("A1:A" & row).Find(txtProdCodeAI.Value) Is Nothing
If keyExists Then
MsgBox txtProdCodeAI.Value & " already exists!", vbCritical
Exit Sub
End If
Sheets("Inventory").Select
Sheets("Inventory").Cells(row, 1) = txtProdCodeAI.Value
Sheets("Inventory").Cells(row, 2) = txtProdNameAI.Value
Sheets("Inventory").Cells(row, 3) = txtQuantityAI.Value
Sheets("Inventory").Cells(row, 4) = txtSupplierAI.Value
Sheets("Inventory").Cells(row, 5) = txtSupplierNumberAI.Value
End Sub
excel中的其他解决方案,您可以在整个列中使用验证格式,在空列中,select整个列中,然后转到数据选项卡 > 数据验证 > 还有:
这将限制您输入的所有值(与主键类似的方案,没有重复)但是它会提示错误(因为您不能输入重复的值),所以在您的循环中您需要添加像这样的错误处理程序 (未测试)
On Error GoTo ErrorHandler
Sheets("Inventory").Select
Sheets("Inventory").Cells(row, 1) = txtProdCodeAI.Value
Sheets("Inventory").Cells(row, 2) = txtProdNameAI.Value
Sheets("Inventory").Cells(row, 3) = txtQuantityAI.Value
Sheets("Inventory").Cells(row, 4) = txtSupplierAI.Value
Sheets("Inventory").Cells(row, 5) = txtSupplierNumberAI.Value
Exit Sub
ErrorHandler:
MsgBox "This Product Code" & txtProdCodeAI.Value & "is a duplicate"
Resume Next
我有一个用户表单,可以将新项目添加到我的库存中 sheet。
如果我可以让我的产品像主键一样,我想寻求帮助。这样 "product key" 就不能重复了。任何帮助将不胜感激。
我的代码在 link 中如下(似乎无法将我的代码粘贴到此框中,因为我一直收到错误消息,说我的代码格式不正确)抱歉不便之处:http://imgur.com/7xFDawO
Option Explicit
Private Sub cmdAdd_Click()
Dim row As Long
Do
row = row + 1
Loop Until (Sheets("Inventory").Cells(row, 1) = "")
Sheets("Inventory").Select
Sheets("Inventory").Cells(row, 1) = txtProdCodeAI.Value
Sheets("Inventory").Cells(row, 2) = txtProdNameAI.Value
Sheets("Inventory").Cells(row, 3) = txtQuantityAI.Value
Sheets("Inventory").Cells(row, 4) = txtSupplierAI.Value
Sheets("Inventory").Cells(row, 5) = txtSupplierNumberAI.Value
End Sub
Private Sub cmdCancel_Click()
Unload AddInventory
End Sub
P.S代码已添加到link中的图片描述中,但格式不正确
**
- 编辑
**
用户通过文本框输入产品代码、产品名称、数量、供应商(名称)和供应商联系电话。
主键位于 A 列并从第 2 行开始,因为有一个 header。
sheet 名字叫 "Inventory"。
如果输入相同的主键,将弹出错误消息"vbokonly - Error! Product code already exists. Kindly use Update feature."
只需检查 A 列中的值:
txtProdCodeAI.Value
像这样:
Dim keyExists as Boolean
keyExists = Not Range("A1:A" & row).Find(txtProdCodeAI.Value) Is Nothing
If keyExists Then
MsgBox txtProdCodeAI.Value & " already exists!", vbCritical
Exit Sub
End If
像这样把它放在你的代码中:
Private Sub cmdAdd_Click()
Dim row As Long
Dim keyExists as Boolean
Do
row = row + 1
Loop Until (Sheets("Inventory").Cells(row, 1) = "")
keyExists = Not Sheets("Inventory").Range("A1:A" & row).Find(txtProdCodeAI.Value) Is Nothing
If keyExists Then
MsgBox txtProdCodeAI.Value & " already exists!", vbCritical
Exit Sub
End If
Sheets("Inventory").Select
Sheets("Inventory").Cells(row, 1) = txtProdCodeAI.Value
Sheets("Inventory").Cells(row, 2) = txtProdNameAI.Value
Sheets("Inventory").Cells(row, 3) = txtQuantityAI.Value
Sheets("Inventory").Cells(row, 4) = txtSupplierAI.Value
Sheets("Inventory").Cells(row, 5) = txtSupplierNumberAI.Value
End Sub
excel中的其他解决方案,您可以在整个列中使用验证格式,在空列中,select整个列中,然后转到数据选项卡 > 数据验证 > 还有:
这将限制您输入的所有值(与主键类似的方案,没有重复)但是它会提示错误(因为您不能输入重复的值),所以在您的循环中您需要添加像这样的错误处理程序 (未测试)
On Error GoTo ErrorHandler
Sheets("Inventory").Select
Sheets("Inventory").Cells(row, 1) = txtProdCodeAI.Value
Sheets("Inventory").Cells(row, 2) = txtProdNameAI.Value
Sheets("Inventory").Cells(row, 3) = txtQuantityAI.Value
Sheets("Inventory").Cells(row, 4) = txtSupplierAI.Value
Sheets("Inventory").Cells(row, 5) = txtSupplierNumberAI.Value
Exit Sub
ErrorHandler:
MsgBox "This Product Code" & txtProdCodeAI.Value & "is a duplicate"
Resume Next