应用程序VLOOKUP没有价值

时间:2016-11-03 10:00:23

标签: excel vba excel-vba excel-formula

我正在根据VLOOKUP代码输入数据,但一直收到错误。

    For Each Cell In Rng
    Cell.Offset(0, 2).Value = Application.WorksheetFunction.VLookup(Cell, Table2, 1, False)

    Next

我希望C列要么发布VLOOKUP值,要么返回一条消息'退回的项目未扫描'。我使用错误处理程序来执行此操作,但是在运行时我一直收到错误。

          'MyErrorHandler:
           ' If Err.Number = 1004 Then
            '     Cell.Offset(0, 2).Value = "Returned Item Not Scanned"
           ' ElseIf Err.Number = 13 Then
           ' MsgBox "Incorrect Exceptions Data."
           ' Else
           '
            ' End If

错误表明'无法获取WorksheetFunction类的VLookup属性。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

尝试下面的代码,我使用Application.VLookup来捕获错误。

(在此方法中捕获VLookup错误时,Err.Number

没有得到1004
Option Explicit

Sub VLookup_with_ErrHandling()

Dim Cell                As Range
Dim Rng                 As Range
Dim Table2              As Range

' modify "Table2" range to your needs
Set Table2 = Sheets("Sheet1").Range("A1:C20")

' modify "Rng" range to your needs
Set Rng = Sheets("Sheet2").Range("A1:A10")

For Each Cell In Rng
    If Not IsError(Application.VLookup(Cell, Table2, 1, False)) Then
        Cell.Offset(0, 2).Value = Application.VLookup(Cell, Table2, 1, False)
    Else
        Cell.Offset(0, 2).Value = "Returned Item Not Scanned"
    End If
Next

End Sub