VBA如何检查userform组合框值是否已存在于数据库中

时间:2013-03-18 15:33:29

标签: excel vba search combobox userform

我在vba自学,我目前正在为我的公司建立一个基于表单的库存控制系统,将材料转换为定制尺寸。由于业务的定制性质取决于客户,所使用的基础材料的尺寸等,在使用基础材料之后可以存在无数不同的切割或成品库存。因此,很难知道该产品是否已经存在于数据库(FinPriceList)中而没有实际滚动并查找它。因此,我正在寻找一种方法来检查产品是否存在于表FinPriceList(分别为A,B,C和D列)中,使用vba交叉引用组合框产品组(FinProdGroup)的输入,产品类别“FinProdCat” ,产品描述“FinProdDesc”和来自userform FinProdUserForm的文本框产品尺寸“FinProdSize”。

我目前的尝试如下,因此任何帮助将不胜感激:

Sub VerifyStock()
Const DQTE = """"

Dim criteria1 As String
Dim criteria2 As String
Dim criteria3 As String
Dim criteria4 As String
Dim evalStr As String
Dim prodCode As Variant

criteria1 = FinProdIn.FinProdGroupIn.Value 'combobox1 from userform
criteria2 = FinProdIn.FinProdCatIn.Value   'combobox2 from userform
criteria3 = FinProdIn.FinProdDescIn.Value  'combobox3 from userform
criteria4 = FinProdIn.FinProdSizeIn.Value  'combobox4 from userform
evalStr = "INDEX(FinishedPriceList,MATCH(" & DQTE & criteria1 & DQTE & "&" & DQTE & criteria2 & DQTE & "&" & DQTE & criteria3 & DQTE & "&" & DQTE & criteria4 & DQTE & ",FinPriceList!$A:$A&FinPriceList!$B:$B&FinPriceList!$C:$C&FinPriceList!$D:$D,0))"
prodCode = Evaluate(evalStr)

If VarType(prodCode) = vbError Then
    MsgBox "Not Found! Would you like to add new product to stock list?", vbYesNo
    If Response = vbNo Then
        MsgBox "Cancelled", vbInformation
        Unload FinProdIn
    Else
    Call ModuleAddtoFinPriceList.AddtoFinPriceList
    End If
Else
    MsgBox "Product found would you like to add a transaction?", vbYesNo
    If Response = vbNo Then
        MsgBox "Cancelled"
        Unload FinProdIn
    Else
        Call ModuleAddTrans.AddTrans
    End If
End If
End Sub

1 个答案:

答案 0 :(得分:1)

IndexMatch工作表函数与Evaluate VBA方法结合使用。例如,如果您的产品代码(或部件号或其他标识符)在A2:A5000范围内,并且您在B2中有两个匹配条件:B5000和C2:C5000,您可以使用:

Sub FindVal()
    Const DQTE = """"

    Dim criteria1 As String
    Dim criteria2 As String
    Dim evalStr As String
    Dim prodCode As Variant

    criteria1 = "ABC"
    criteria2 = "DEF"
    evalStr = "INDEX($C$2:$C$5000,MATCH(" & DQTE & criteria1 & DQTE & "&" & DQTE & criteria2 & DQTE & ",$A$2:$A$5000&$B$2:$B$5000,0))"

    prodCode = Evaluate(evalStr)

    If VarType(prodCode) = vbError Then
        MsgBox "No match found"
    Else
        MsgBox "Value found: " & prodCode
    End If
End Sub