[英]Excel 2013 on W10: VBA Vlookup in a different worksheet produces error 1004
I'm developing an application which uses specification codes to vlookup those codes in another spreadsheet, and return vendor numbers from the second spreadsheet to the first, listing them in the same column with the specification code. 我正在开发一个应用程序,该应用程序使用规范代码在另一个电子表格中查找这些代码,并将供应商编号从第二个电子表格返回到第一个电子表格,并将它们与规范代码列在同一列中。
ActiveCell.FormulaR1C1 = Application.WorksheetFunction.VLookup(specsec, [Vendorspec.xlsx!vid], 4)
in the above code line: 在上面的代码行中:
The code line produces error "Run-time error '1004': Unable to get the vlookup property of the worksheet function class". 代码行产生错误“运行时错误'1004':无法获取工作表函数类的vlookup属性”。
Can anyone suggest a fix? 任何人都可以提出修复建议吗?
Thank you. 谢谢。
Your lookup is simply failing, and the error message is utterly misleading. 您的查找完全失败,并且错误消息完全令人误解。
It's not that VBA couldn't find the WorksheetFunction.VLookup
member, it's just that your VLookup raised an error. 这并不是说VBA找不到
WorksheetFunction.VLookup
成员,而是因为您的VLookup引发了错误。
You need to either: 您需要:
On Error GoTo
statement On Error GoTo
语句处理该运行时错误 Or 要么
Application.VLookup
, which doesn't give you IntelliSense , but instead of throwing a runtime error when the lookup fails, it will return "Error 2042" and you can test whether the lookup failed or not by wrapping it in IsError
. Application.VLookup
,它没有给您IntelliSense ,但不是在查找失败时引发运行时错误,而是返回“错误2042”,并且您可以通过将其包装来测试查找是否失败在IsError
。 Type 42
in cell A1
of the active sheet. 在活动工作表的单元格
A1
中键入42
。 Then in the immediate pane : 然后在即时窗格中 :
?iserror(application.VLookup(42,Range("A:B"),1,false))
returns False
返回
False
?iserror(application.VLookup(43,Range("A:B"),1,false))
returns True
返回
True
?application.WorksheetFunction.VLookup(42,Range("A:B"),1,false)
returns 42
返回
42
?application.WorksheetFunction.VLookup(43,Range("A:B"),1,false)
raises a runtime error: 引发运行时错误:
That message would be better worded as "VLookup function failed to find specified lookup value in specified lookup range", or something like that. 该消息最好用“ VLookup函数无法在指定的查找范围内找到指定的查找值”或类似的措词更好地表述。
The reason your lookup is failing is the same any VLOOKUP might fail for: verify your lookup_value
actually exists in your lookup_range
. 查找失败的原因与任何VLOOKUP可能失败的原因相同:验证
lookup_value
实际上存在于lookup_range
。 Watch out for leading and/or trailing spaces, and "text-formatted" columns. 注意前导和/或尾随空格,以及“文本格式”列。 In other words, assuming you want to throw a runtime error when the lookup fails, it's a data problem, not a code problem.
换句话说,假设您想在查找失败时引发运行时错误,那是数据问题,而不是代码问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.