英文:
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()
'Whether v1 and/or v2 are quoted depends on if numeric or text
' here <v1> is numeric and <v2> is not
Const FRM As String = "XLOOKUP(<v1>&<v2>,E:E&AI:AI,H:H)"
Dim res, f
'replace the placeholders in the formula with values from your form
f = Replace(FRM, "<v1>", val(SalesForm.BHSDMAINNUMBERLF.value))
f = Replace(f, "<v2>", SalesForm.BHSDRECORDTD.value)
'evaluate in context of sheet TELEDATA
res = Worksheets("TELEDATA").Evaluate(f)
If IsError(res) Then 'if got an error then no match was made
Debug.Print "No match"
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("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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论