我有以下脚本,我收到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"
这很奇怪,因为所有变量和范围在监视模式下都很好。查询也是字母的...任何想法发生了什么?
答案 0 :(得分:5)
这可能意味着任何事情。这可能只是意味着在LookupRange中找不到您的Data(i, 4)
值。
Run-time error '1004':
Unable to get the VLookup property of the WorksheetFunction class
与从#N/A
=vlookup("A",A1:B3,2,false)
无效
在行
上设置断点test(i) = Application.WorksheetFunction.VLookup(Data(i, 4), LookupRange, 3, 0)
并在Data(i, 4)
上设置监视以及在i
上设置监视。查看Data(i, 4)
中的值是否存在于查找范围内。查看i
是否大于1,如果它已正确运行了一些循环迭代。
作为旁注,您的代码无论如何都不会运行,因为Test
是一个空变量而不是数组。你需要像
ReDim Test(LBound(Data, 1) To UBound(Data, 1))
在for循环之前使其工作。
阅读错误处理here。您需要从VBA正确处理VLOOKUP。