VBA - vlookup正在运行运行时错误'13'

时间:2016-07-20 09:25:13

标签: excel vba macros runtime-error vlookup

我是VBA的新手,我想创建一个VLOOKUP宏,但因此面临同样的问题,当VLOOKUP无法找到它运行的范围内的值时,运行错误'13'类型不匹配。

我希望宏做的是对于TOTAL表中的每个行项找到OLA列表表中的值并且它执行它,除了那些未包含在OLA列表表中的那些,对于他们我希望它写“NA”。

请参阅下面的代码:

Sub elsovlookup()

    Dim i As Long
    Dim test As String
    Dim size As Long

    size = WorksheetFunction.CountA(Worksheets("TOTAL").Columns(1)) + 1

        For i = 3 To size

        cell = Worksheets("TOTAL").Cells(i, "U")
        test = Application.VLookup(cell, Worksheets("OLA list").Range("K:R"), 8, False)
        If IsError(test) Then
            test = "NA"
            Worksheets("TOTAL").Cells(i, "AC") = "OLA not found"
        Else:
            Worksheets("TOTAL").Cells(i, "AC") = "OLA found"
        End If
        Worksheets("TOTAL").Cells(i, "V") = test
    Next i

    MsgBox "First vlookup is complete."

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码。它会起作用

Sub elsovlookup()
    Dim i As Long
    Dim size As Long
    Dim cell As Range
    size = WorksheetFunction.CountA(Worksheets("TOTAL").Columns(1)) + 1
    For i = 3 To size
        Set cell = Worksheets("TOTAL").Cells(i, "U")
        If Application.WorksheetFunction.CountIf(Worksheets("OLA list").Range("K:K"), cell.Value) > 0 Then
            Worksheets("TOTAL").Cells(i, "AC") = "OLA found"
        Else
            Worksheets("TOTAL").Cells(i, "AC") = "OLA not found"
        End If
        Worksheets("TOTAL").Cells(i, "V") = test
    Next i
    MsgBox "First vlookup is complete."
End Sub