当项目在列表中时,Vlookup返回错误

问题:项目在VLOOKUP表中,但在适当的单元格中返回“Missing”的值。 当conud打印,如果我复制粘贴它从debugging窗口和Ctrl + F – 发现它的J列find它没有问题。 为什么按Ctrl + F而不是VLookup?

笔记:

  • 这只是相关的代码,而不是整个块。
  • lrVelocity正在计算正确。 价值是1,951。
  • 值的forms为0001HCM8889W01,因此不会违反VLOOKUP最大字符数限制。
  • 正如你所看到的,我试图修剪任何不可见的空间,并确保它们都是string。
  • 我对VBA来说是新手,对于这个问题的所有帮助表示感谢。 我阅读了多篇Google文章,但是没有一篇修复版本解决了我的问题。
Option Explicit Dim wsMain As Worksheet Dim wsQuantity As Worksheet Dim wsVelocity As Worksheet Dim wsParameters As Worksheet Dim wsData As Worksheet Dim lrMain As Long 'lr = last row Dim lrQuantity As Long Dim lrVelocity As Long Dim lrParameters As Long Dim lrData As Long Dim conUD As String 'con=concatenate Dim conECD As String Dim calcWeek As Long Dim RC As Long 'Row Counter Dim vl As Variant 'Vlookup, Variant to allow for errors without breaking the code calcWeek = wsParameters.Range("B3").Value lrVelocity = wsVelocity.Cells.Find(What:="*", SearchOrder:=xlByRows, Searchdirection:=xlPrevious).Row Set wsMain = Worksheets("Main Tab") Set wsVelocity = Worksheets("Velocity") For RC = 2 To 10 'lrVelocity With wsVelocity .Cells(RC, 10) = .Cells(RC, 1) & .Cells(RC, 1) & .Cells(RC, 4) & .Cells(RC, 5) & .Cells(RC, 9) .Cells(RC, 10).Value = CStr(Trim(.Cells(RC, 10).Value)) .Cells(RC, 11) = .Cells(RC, 6) .Cells(RC, 12) = .Cells(RC, 7) .Cells(RC, 13) = .Cells(RC, 8) .Cells(RC, 14) = .Cells(RC, 3) .Cells(RC, 22) = .Cells(RC, 1) & .Cells(RC, 9) End With Next RC For RC=2 To 10 conUD = wsMain.Cells(RC, 21) & wsMain.Cells(RC, 4) & calcWeek conUD = CStr(Trim(conUD)) Debug.Print conUD wsVelocity.Activate vl = Application.VLookup(conUD, wsVelocity.Range(wsVelocity.Cells(2, 10), wsVelocity.Cells(lrVelocity, 11)), 2, False) If IsError(vl) Then wsMain.Cells(RC, 10).Value = "Missing" Else wsMain.Cells(RC, 10).Value = vl End If Next RC 

我认为@ user1274820是一些东西。 通常我们使用的Application.Vlookup因为你正在使用的预期,可能值不会在table_array的第一列中find,你想要在输出中处理“缺less”值。

但是 ,如果find该值,但返回列(k,在你的情况)中的值是一个错误,那么函数也会返回错误。 在你的情况下,当在J列中find该值时,看起来K列包含#N/A (让我知道如果不是这样的话!)

在这两种情况下, Application.Vlookup返回Error 2042

  1. table_array的第一列中找不到lookup_value (最常见的用法和期望,IMO)
  2. find了lookup_value ,但col_index_num的结果值本身就是一个错误。

因此,即使查找值存在,如果返回值可能包含错误,那么我们不能使用Application.Vlookup来testing值的存在, 但是可以使用另一种方法,WorksheetFunction.CountIfApplication.Match

在这里,我们只需查询J列并使用CountIf来确保至less有一个匹配的值。 这将提前validation我们的Vlookup ,但是我们仍然需要处理返回值中可能出现的错误。

 For RC = 2 to 10 conUD = wsMain.Cells(RC, 21) & wsMain.Cells(RC, 4) & calcWeek conUD = CStr(Trim(conUD)) Debug.Print conUD With wsVelocity Dim lookupRange as Range Set lookupRange = .Range(.Cells(2, 10), .Cells(lrVelocity, 11)) End With If Application.WorksheetFunction.CountIf(lookupRange.Columns(1), conUD) <> 0 Then 'The value is found, it should be safe to use VLOOKUP vl = Application.VLookup(conUD, lookupRange, 2, False) '## Handles an error in the return value from the return column If IsError(vl) Then '## Copies the error from return column, or modify as needed wsMain.Cells(RC, 10).Value = CVerr(vl) Else '## Value found in Col J and return Vlookup from Col K wsMain.Cells(RC, 10).Value = vl End If Else '## Value NOT FOUND in column J wsMain.Cells(RC, 10).Value = "Missing" End If Next 

更新

从聊天中,我可以看到您的Main和Lookup表格的值的格式是不同的。 在你的查询表中,你正在复制一个前缀,例如“0001HCM8889”,所以你最终得到“0001HCM8890001HCM889W01”。

这就是为什么Find或Ctrl + F会find单元格,但VLOOKUP不会,因为它需要一个精确的匹配。

看起来你正在构build/消毒第一个循环中的查找表,你应该可以通过这样做来修复它:

 For RC = 2 To 10 'lrVelocity With wsVelocity '## Removed the duplicate .Cells(RC, 1) from the next line ## .Cells(RC, 10) = .Cells(RC, 1) & .Cells(RC, 4) & .Cells(RC, 5) & .Cells(RC, 9) .Cells(RC, 10).Value = CStr(Trim(.Cells(RC, 10).Value)) .Cells(RC, 11) = .Cells(RC, 6) .Cells(RC, 12) = .Cells(RC, 7) .Cells(RC, 13) = .Cells(RC, 8) .Cells(RC, 14) = .Cells(RC, 3) .Cells(RC, 22) = .Cells(RC, 1) & .Cells(RC, 9) End With Next RC