为什么我的Application.XLookup在VBA中出现“类型不匹配”错误?

huangapple go评论146阅读模式
英文:

Why is my Application.XLookup in VBA giving "Type Mismatch" Error?

问题

修改后的代码使用布尔逻辑而不是连接字符串,但仍然遇到相同的错误。

我尝试在VBA中使用application.xlookup函数,并通过在此代码内部连接公式来添加额外的条件。

Sub employeelookup()
    SalesForm.BHSDEMPLOYEETD.Value = Application.XLookup(1, (Worksheets("TELEDATA").Range("E:E") = SalesForm.BHSDMAINNUMBERLF.Value) * (Worksheets("TELEDATA").Range("AI2:AI5") = SalesForm.BHSDRECORDTD.Value), Worksheets("TELEDATA").Range("F:F"))
End Sub

我尝试更改括号和调整顺序,但是出现了语法错误。理论上,上述代码应该可以工作,但是我无法找出Type Mismatch错误的来源。

英文:

EDIT: Changed my code to use boolean logic instead of concatenating but am still getting the same error.

I am trying to use the application.xlookup in vba, and add extra criteria by concatenating the formula inside this code.

Sub employeelookup()
 SalesForm.BHSDEMPLOYEETD.Value = Application.XLookup(1, (Worksheets("TELEDATA").Range("E:E") = SalesForm.BHSDMAINNUMBERLF.Value) * (Worksheets("TELEDATA").Range("AI2:AI5") = SalesForm.BHSDRECORDTD.Value), Worksheets("TELEDATA").Range("F:F"))
End Sub

I have tried changing the parentheses and messing with the order but I was getting syntax errors. The code above should work in theory but I can't figure out where the Type Mismatch error is coming from.

答案1

得分: 2

你可以使用Evaluate

Sub employeelookup()
    '无论v1和/或v2是否带引号取决于它们是数值还是文本
    '在这里,<v1>是数值,<v2>不是
    Const FRM As String = "XLOOKUP(<v1>&<v2>,E:E&AI:AI,H:H)"
    Dim res, f
    
    '用表单中的值替换公式中的占位符
    f = Replace(FRM, "<v1>", Val(SalesForm.BHSDMAINNUMBERLF.Value))
    f = Replace(f, "<v2>", SalesForm.BHSDRECORDTD.Value)
    
    '在TELEDATA工作表上下文中计算公式
    res = Worksheets("TELEDATA").Evaluate(f)
    
    If IsError(res) Then '如果出现错误,则没有匹配项
        Debug.Print "没有匹配"
    Else
        Debug.Print res
    End If
End Sub
英文:

You could use Evaluate:

Sub employeelookup()
    &#39;Whether v1 and/or v2 are quoted depends on if numeric or text
    &#39;  here &lt;v1&gt; is numeric and &lt;v2&gt; is not
    Const FRM As String = &quot;XLOOKUP(&lt;v1&gt;&amp;&lt;v2&gt;,E:E&amp;AI:AI,H:H)&quot;
    Dim res, f
    
    &#39;replace the placeholders in the formula with values from your form
    f = Replace(FRM, &quot;&lt;v1&gt;&quot;, val(SalesForm.BHSDMAINNUMBERLF.value))
    f = Replace(f, &quot;&lt;v2&gt;&quot;, SalesForm.BHSDRECORDTD.value)
    
    &#39;evaluate in context of sheet TELEDATA
    res = Worksheets(&quot;TELEDATA&quot;).Evaluate(f)
    
    If IsError(res) Then  &#39;if got an error then no match was made
        Debug.Print &quot;No match&quot;
    Else
        Debug.Print res
    End If
End Sub

答案2

得分: 2

你可以将所有内容放入数组中,然后循环查找数值:

Sub employeelookup()
    Dim mn_num As Double
    mn_num = Val(SalesForm.BHSDMAINNUMBERLF.Value)
    Dim rec As String
    rec = SalesForm.BHSDRECORDTD.Value
    
    Dim lkpmnArr As Variant
    lkpmnArr = Intersect(Worksheets("TELEDATA").UsedRange, Worksheets("TELEDATA").Range("E:E")).Value
    
    Dim lkprecArr As Variant
    lkprecArr = Intersect(Worksheets("TELEDATA").UsedRange, Worksheets("TELEDATA").Range("AI:AI")).Value
    
    Dim lkotArr As Variant
    lkotArr = Intersect(Worksheets("TELEDATA").UsedRange, Worksheets("TELEDATA").Range("F:F")).Value
    
    Dim otpt As String
    otpt = "Not Found"
    
    Dim i As Long
    For i = LBound(lkpmnArr, 1) To UBound(lkpmnArr, 1)
        If lkpmnArr(i, 1) = mn_num And lkprecArr(i, 1) = rec Then
            otpt = lkotArr(i, 1)
            Exit For
        End If
    Next i
    
    SalesForm.BHSDEMPLOYEETD.Value = otpt
End Sub
英文:

You can put everything in arrays and loop those to find the value:

Sub employeelookup()
    Dim mn_num As Double
    mn_num = Val(SalesForm.BHSDMAINNUMBERLF.Value)
    Dim rec As String
    rec = SalesForm.BHSDRECORDTD.Value
    
    Dim lkpmnArr As Variant
    lkpmnArr = Intersect(Worksheets(&quot;TELEDATA&quot;).UsedRange, Worksheets(&quot;TELEDATA&quot;).Range(&quot;E:E&quot;)).Value
    
    Dim lkprecArr As Variant
    lkprecArr = Intersect(Worksheets(&quot;TELEDATA&quot;).UsedRange, Worksheets(&quot;TELEDATA&quot;).Range(&quot;AI:AI&quot;)).Value
    
    Dim lkotArr As Variant
    lkotArr = Intersect(Worksheets(&quot;TELEDATA&quot;).UsedRange, Worksheets(&quot;TELEDATA&quot;).Range(&quot;F:F&quot;)).Value
    
    Dim otpt As String
    otpt = &quot;Not Found&quot;
    
    Dim i As Long
    For i = LBound(lkpmnArr, 1) To UBound(lkpmnArr, 1)
        If lkpmnArr(i, 1) = mn_num And lkprecArr(i, 1) = rec Then
            otpt = lkotArr(i, 1)
            Exit For
        End If
    Next i
    
    
    SalesForm.BHSDEMPLOYEETD.Value = otpt
End Sub

huangapple
  • 本文由 发表于 2023年4月20日 00:35:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定