【问题标题】:Prevent duplicate values from userform to table column防止从用户表单到表列的重复值
【发布时间】:2016-08-14 02:33:58
【问题描述】:

我有一个用户表单可以将客户信息输入到表格中。我有这个是为了防止没有名字的客户进入:

If CBCustName.Text = "" Then

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

   CBCustName.SetFocus
   Exit Sub
End If

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

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

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

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

【问题讨论】:

    标签: excel vba duplicates userform


    【解决方案1】:

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

    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
    

    【讨论】:

    • 太棒了!谢谢cyboshu。我修改了您的代码以使用表:“Set rngCust = CustInfoSheet.ListObjects("CustInfo").ListColumns(1).DataBodyRange.Find(CustName)”,效果很好!再次感谢您抽出时间提供帮助。
    • 哦,对不起,马科斯。几天来,我一直在努力寻找答案。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    相关资源
    最近更新 更多