简体   繁体   English

W10上的Excel 2013:不同工作表中的VBA Vlookup产生错误1004

[英]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: 在上面的代码行中:

  • specsec is the specification code, of the form XX XX XX.XX specsec是规范代码,格式为XX XX XX.XX
  • The name of the vlookup target file is "VendorSpec.xlsx". vlookup目标文件的名称是“ VendorSpec.xlsx”。 In this worksheet, each code is a unique entry in column 1, with the first of several vendor numbers in column 4. Future code will cycle through the columns, returning all subsequent vendor IDs for the current code. 在此工作表中,每个代码在第1列中都是一个唯一的条目,在第4列中是多个供应商编号的第一个。将来的代码将在各列中循环,返回当前代码的所有后续供应商ID。

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: 您需要:

  • Handle that runtime error with an On Error GoTo statement 使用On Error GoTo语句处理该运行时错误

Or 要么

  • Use the late-bound version 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: 引发运行时错误:

无法获取WorksheetFunction类的VLookup属性

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM