Excel VBA Vlookup – 无法获取Vlookup属性

我有以下脚本,我得到一个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" 

这很奇怪,因为所有的variables和范围在手表模式下看起来都很好。 查找也是字母…任何想法是怎么回事?

这可能意味着任何事情。 它可能仅仅意味着你的Data(i, 4)值在LookupRange中找不到。

 Run-time error '1004': Unable to get the VLookup property of the WorksheetFunction class 

等同于#N #N/A From =vlookup("A",A1:B3,2,false)

在线上设置一个断点

 test(i) = Application.WorksheetFunction.VLookup(Data(i, 4), LookupRange, 3, 0) 

并在Data(i, 4)上设置一个手表,并在Data(i, 4)上设置一个手表。 查看Data(i, 4)的值是否存在于查找范围中。 看看i是否大于1,如果它正确地运行了一些循环迭代。

作为一个方面说明你的代码不会运行,因为Test是一个空的变体而不是一个数组。 你需要一个像

 ReDim Test(LBound(Data, 1) To UBound(Data, 1)) 

在for循环之前它工作。

阅读error handling在这里 。 您需要从VBA正确处理VLOOKUP。