防止从用户表单到 table 列的重复值

Prevent duplicate values from userform to table column

我有一个用户表单可以将客户信息输入 table。我这样做是为了防止没有姓名的客户进入:

If CBCustName.Text = "" Then

   MsgBox "Nothing to Add.  Enter Customer Information.", vbOKOnly, "Enter Customer Data"

   CBCustName.SetFocus
   Exit Sub
End If

我还想查看客户是否已经存在(table CustInfo 的 A 列),并显示一个消息框告诉用户不允许重复。客户名称输入组合框 CBCustName。

此例程由命令按钮 CmdAddNewCust 启动。在这两个检查都是运行之后,应该将用户输入到控件的所有数据写入到table的相应row/column中。我有这个来做那个部分,它似乎工作正常:

Set tblRow = CustInfoTable.ListRows

tblRow.Range(1, 1).Value = CBCustName.Value
tblRow.Range(1, 2).Value = TxtAddress.Value
tblRow.Range(1, 3).Value = TxtCity.Value
tblRow.Range(1, 4).Value = TxtState.Value
tblRow.Range(1, 5).Value = TxtZip.Value
tblRow.Range(1, 6).Value = TxtPhone.Value
tblRow.Range(1, 7).Value = TxtContact.Value
tblRow.Range(1, 8).Value = TxtULRate.Value
tblRow.Range(1, 9).Value = TxtLRate.Value
tblRow.Range(1, 10).Value = TxtStandby.Value
tblRow.Range(1, 11).Value = TxtFuelSCharge.Value

我试过修改几个代码片段,但我遗漏了一些东西。谁能指出我正确的方向以防止重复输入?一如既往,非常感谢您的帮助。

您需要预先搜索客户的范围并检查客户名称是否已存在,例如:

dim rngCust as range

set rngCust = thisworkbook.sheets("SheetName").Range("A:A").Find(CustomerName) 

if rngCust is nothing then     
   addCustomer    
else    
   msgbox "Customer already exists"    
end if