我有以下脚本,并且收到VLOOKUP错误:
Dim DataRange, LookupRange As Range
Dim Data, Test As Variant
Set DataRange = Sheets("sheet").Range("A1:K12000")
Set LookupRange = sheets("sheet2").Range("A1:C50")
Data = DataRange.Value
For i = LBound(Data, 1) To UBound(Data,1)
ReDim Preserve Test(1 To 3, 1 To i)
test(1, i) = Application.WorksheetFunction.VLookup(Data(i, 4), LookupRange, 3, 0)
'Other stuff works fine
Next i不幸的是,我得到的错误是:
"Unable to get the VLookup property of the WorksheetFunction class"这很奇怪,因为所有的变量和范围在手表模式下看起来都很好。查找也是按字母顺序的.知道是怎么回事吗?
发布于 2013-08-28 01:05:23
这可能意味着许多事情。这可能仅仅意味着在LookupRange中找不到您的Data(i, 4)值。
Run-time error '1004':
Unable to get the VLookup property of the WorksheetFunction class等同于从=vlookup("A",A1:B3,2,false)获取#N/A
在行上设置断点
test(i) = Application.WorksheetFunction.VLookup(Data(i, 4), LookupRange, 3, 0)和i一样,在Data(i, 4)上设置一个手表。查看Data(i, 4)中的值是否存在于您的查找范围内。查看i是否大于1,看看它是否正确地运行了循环的一些迭代。
顺便说一下,你的代码无论如何都不会运行,因为Test是一个空的变量,而不是数组。你需要像这样的一行
ReDim Test(LBound(Data, 1) To UBound(Data, 1))在for循环使其工作之前。
阅读错误处理here。为了从VBA中正确地处理VLOOKUP,您将需要它。
https://stackoverflow.com/questions/18471266
复制相似问题