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

我有一个用户表单将客户信息input到表格中。 我有这个防止没有名字的客户进入:

If CBCustName.Text = "" Then MsgBox "Nothing to Add. Enter Customer Information.", vbOKOnly, "Enter Customer Data" CBCustName.SetFocus Exit Sub End If 

我也想看看客户是否已经存在(表CustInfo列A),并显示一个消息框告诉用户重复不被允许。 客户名称被input到comboboxCBCustName中。

这个例程由命令buttonCmdAddNewCust启动。 运行这两个检查之后,它应该将用户input到控件的所有数据写入表的相应行/列。 我有这个做这个部分,它似乎工作正常:

 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 

我试过修改几个代码片段,但是我错过了一些东西。 任何人都可以指向正确的方向来防止重复的条目? 一如既往,您的帮助非常感谢。

您需要预先search与客户的范围,并检查客户名称是否已经存在,如下所示:

 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 
Interesting Posts