【问题标题】:VBA - vlookup is running to runtime error '13'VBA - vlookup 正在运行到运行时错误“13”
【发布时间】:2016-07-20 09:25:13
【问题描述】:

我对 VBA 相当陌生,我想创建一个 VLOOKUP 宏,但因此面临同样的问题,当 VLOOKUP 无法从运行时错误“13”类型不匹配的范围中找到值时。

我想要宏做的是为 TOTAL 表中的每个行项目从 OLA 列表表中找到值,它会执行此操作,除了 OLA 列表表中未包含的那些,对于他们我希望它写“不”。

请参阅下面的代码:

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."

提前感谢您的帮助

【问题讨论】:

  • 当您调试时,您的“单元格”返回的是什么?我的显示为空,尽管这可能与我输入的虚拟数据有关。我原以为这是因为 Worksheets.Cells 期望 rowindex 和 columnindex 作为它的参数,而您使用“U”作为字符串代替一个数字。
  • Application.VLookup 将返回 Variant。这是成功的VLOOKUPed 单元格的值或Error 值。但是Error 值不适合String。这就是需要Dim test As Variant 的原因。
  • @Tim Edwards:Worksheets("TOTAL").Cells(i, "U") 中的“U”表示U 列。所以你必须在Worksheets("TOTAL") 中放入一些东西到U3 中。如果i 是 3,那么你会得到它。
  • 谢谢@AxelRichter,我只使用了带有数字的单元格(),不知道你可以混合,因为我很长。
  • 谢谢@AxelRichter,工作就像一个魅力!

标签: excel vba macros runtime-error vlookup


【解决方案1】:

试试下面的代码。它会工作

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

【讨论】:

    猜你喜欢
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多