Excel VBA-在内部使用VlookUp是错误的

Excel VBA-在内部使用VlookUp是错误的,excel,vlookup,vba,Excel,Vlookup,Vba,我需要对一系列单元格进行vlookup运算,根据它返回的值或a#N/a,我想对它进行一些操作 我试图将vlookup放在iError函数中,但没有成功 这是我下一步尝试的,但也不起作用: Set costCentreMapping = Workbooks.Open(Filename:="C:\mapping.xlsx") Sheets("Sheet2").Activate Dim CostCentreRange As Range Set CostCentreRange = Range("A4:E

我需要对一系列单元格进行vlookup运算,根据它返回的值或a#N/a,我想对它进行一些操作

我试图将vlookup放在iError函数中,但没有成功

这是我下一步尝试的,但也不起作用:

Set costCentreMapping = Workbooks.Open(Filename:="C:\mapping.xlsx")
Sheets("Sheet2").Activate
Dim CostCentreRange As Range
Set CostCentreRange = Range("A4:E2967")

    Set test1 = Application.WorksheetFunction.VLookup(appid, CostCentreRange, 2, False)

            If IsError(test1) Then 

                appid.Offset(columnoffset:=15) = "value1"

            End If
你建议我做什么

谢谢

您的整个代码(您在上面发布的)可能看起来像下面的较短版本:

If IsError(Application.VLookup(appid, CostCentreRange, 2, False)) Then ' <-- not found in VLookup
    appid.Offset(, 15) = "value1"

    If customercountry = "UNITED KINGDOM" Then
        If IsError(Application.VLookup(billService, billableRange, 3, False)) Then
            appid.Offset(, 14) = "value2"
        End If
    End If
End If
如果IsError(Application.VLookup(appid,CostCentreRange,2,False)),那么“您的整个代码(您在上面发布的)可能看起来像下面的较短版本:

If IsError(Application.VLookup(appid, CostCentreRange, 2, False)) Then ' <-- not found in VLookup
    appid.Offset(, 15) = "value1"

    If customercountry = "UNITED KINGDOM" Then
        If IsError(Application.VLookup(billService, billableRange, 3, False)) Then
            appid.Offset(, 14) = "value2"
        End If
    End If
End If

如果IsError(Application.VLookup(appid,CostCentreRange,2,False))那么“正如我在评论中所说:

Set costCentreMapping = Workbooks.Open(Filename:="C:\mapping.xlsx")

Dim CostCentreRange As Range
Set CostCentreRange = costCentreMapping.Sheets("Sheet2").Range("A4:E2967")

Dim test1 as variant 'the variable must be a variant to accept an error.
    test1 = Application.VLookup(appid, CostCentreRange, 2, False)

            If IsError(test1) Then 

                appid.Offset(columnoffset:=15) = "value1"

            End If

正如我在评论中所说:

Set costCentreMapping = Workbooks.Open(Filename:="C:\mapping.xlsx")

Dim CostCentreRange As Range
Set CostCentreRange = costCentreMapping.Sheets("Sheet2").Range("A4:E2967")

Dim test1 as variant 'the variable must be a variant to accept an error.
    test1 = Application.VLookup(appid, CostCentreRange, 2, False)

            If IsError(test1) Then 

                appid.Offset(columnoffset:=15) = "value1"

            End If


为此,您需要从两个VLookup中删除
.WorksheetFunction
。这将允许将错误传递到变量中。VLOOKUP不返回值吗?我认为您不必“设置”test1和test2(尽管未测试),所以只需Application.VLookup?@AntónioPires使用
VLookup
设置什么?你想从中得到什么值?@ShaiRado我正在使用第一个参数的值在另一个表中查找它。如果该值在该表中不存在,我想执行一些操作;如果该值存在,我想执行一些不同的操作。要执行此操作,您需要从两个VLookup中删除
.WorksheetFunction
。这将允许将错误传递到变量中。VLOOKUP不返回值吗?我认为您不必“设置”test1和test2(尽管未测试),所以只需Application.VLookup?@AntónioPires使用
VLookup
设置什么?你想从中得到什么值?@ShaiRado我正在使用第一个参数的值在另一个表中查找它。如果该值在该表中不存在,我想执行一些操作,如果它存在,我想执行一些不同的操作,这就是我第一次尝试的结果。但是它会导致一个错误,它不会运行。如果您的范围对象是
Nothing
@DavidZemens您是指我还是Antonio?@ShaiRado两者:)如果无法计算范围参数,这将给出错误5无效的过程调用或参数,然后,即使将
应用程序.Vlookup
包装到
iError
函数中,仍会引发错误。我得到一个运行时错误1004:无法获取worksheetfunction类的Vlookup属性这是我第一次尝试的结果。但是它会导致一个错误,它不会运行。如果您的范围对象是
Nothing
@DavidZemens您是指我还是Antonio?@ShaiRado两者:)如果无法计算范围参数,这将给出错误5无效的过程调用或参数,然后,即使将
应用程序.Vlookup
包装到
iError
函数中,仍然会引发错误。我得到一个运行时错误1004:无法获取工作表函数类的Vlookup属性就是这样!谢谢,就是这样!谢谢