我正在尝试使用VLOOKUP检查我的变量是否出现在listobject中,如果是,则检索相应的值。
我的代码总是在以下行出错:
shtData.Cells(rowNumber, perf1).Value = wsFunc.IfError(wsFunc1.VLookup(shtData.Cells(rowNumber, cep1).Value, perfTable, 2, False), Null)
我的代码如下:
Dim wb As Workbook
Set wb = ActiveWorkbook
Dim wsFunc As WorksheetFunction
Set wsFunc = Application.WorksheetFunction
Dim wsFunc1 As WorksheetFunction
Set wsFunc1 = Application.WorksheetFunction
Dim cep As Variant
Dim perfTable As ListObject
Set perfTable = perfData.ListObjects("PerfTable")
Dim cepTable As ListObject
Set cepTable = perfData.ListObjects("cepTable")
perfCol = perfTable.ListColumns("Column1").Index
perfSort = perfTable.ListColumns("Column2").Index
cepCol = cepTable.ListColumns("Column1").Index
cepSort = cepTable.ListColumns("Column2").Index
On Error GoTo errHandler
Set usedRange = returnUsedRange(shtData, wb)
Set perfRange = returnUsedRange(perfData, wb)
Set cepRange = returnUsedRange(perfData, wb)
For Each Row In usedRange
For Each sec In secRange
rowNumber = Row.Row
secRow = sec.Row
shtData.Cells(rowNumber, perf1).Value = wsFunc.IfError(wsFunc1.VLookup(shtData.Cells(rowNumber, cep1).Value, perfTable, 2, False), Null)
Next
答案 0 :(得分:1)
你不能像这样使用excel Iferror函数:
只需使用
x = application.vlookup(...,...,...,...)
没有WorksheetFunction,所以你可以检查Vlookup是否有错误......
if iserror(X) then ' vlookup failed (x is variant)
答案 1 :(得分:0)
为什么不使用这种方法。它快速且易于实施:
Dim i As Integer
Dim arrData2Search() As Variant
Set arrData2Search = Range(Cells(1, 1), Cells(1000, 2000)).value
For i = 1 To 2000
If arrData2Search (i, 1)= "Target" Then
' your code
End If
Next i