首页 > 解决方案 > 如何解决索引/匹配返回错误的问题?

问题描述

我正在尝试使用索引/匹配组合从定义的表“tblPlant1”中引入一个值。问题在于 Match 函数的“错误 2015”和 Index 函数的问题。我最终想将匹配结果合并到 Index 函数中。我了解错误 2015 的 <255 限制,但很难理解它是如何根据查找值和查找数组得出错误的。代码、Debug.Print 输出和表格如下。提前致谢。

Dim plantcode As String
Dim tblPlant1 As ListObject
Dim matchresult As Variant
Dim errormsg As String
Dim VIN As String
Dim Plant1 As Variant

Set tblPlant1 = ThisWorkbook.Sheets(1).ListObjects("tblPlant1")

VIN = ActiveCell.Value
plantcode = Mid(VIN, 11, 1)
errormsg = "Error: Plant Code " & Chr(34) & plantcode & Chr(34) & " (VIN Digit 11) Not Found"


matchresult = Application.Match(plantcode, tblPlant1.ListColumns("VIN"), 0)

Plant1 = WorksheetFunction.IfError(Application.Index(tblPlant1.ListColumns("Plant"), 6), errormsg)


Debug.Print errormsg
Debug.Print VIN
Debug.Print plantcode
Debug.Print matchresult
Debug.Print Plant1

调试的输出如下:

错误:未找到工厂代码“H”(VIN 数字 11)

1HTKSSWK9KH067162

H

错误 2015

错误:未找到工厂代码“H”(VIN 数字 11)

表格示例

标签: excelvbamatchlookup

解决方案


推荐阅读