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

时间:2017-03-16 14:31:51

标签: excel vba excel-vba

问题:项目在VLOOKUP表中,但是返回"缺少"的值。在适当的细胞中。当conUD打印时,如果我从调试窗口复制粘贴它并按Ctrl + F - 在J列上找到它,它会发现它没有问题。为什么通过Ctrl + F而不是VLookup找到它?

注意:

  • 这只是相关代码,而不是整个块。
  • lrVelocity正确计算。价值是1,951。
  • 值为0001HCM8889W01,因此不违反VLOOKUP最大字符限制。
  • 正如你所看到的,我试图修剪任何不可见的空间,并确保它们都是字符串。
  • 我对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

1 个答案:

答案 0 :(得分:1)

我认为@ user1274820正在做点什么。通常我们会在您使用时使用Application.Vlookup,期望可能在table_array的第一列中找不到该值,并且您希望在输出中使用“Missing”值处理该值

HOWEVER ,如果找到值 ,但返回列(k,在您的情况下)中的值是错误,则该函数将返回错误同样。在您的情况下,虽然在列J中找到值 ,但似乎列K包含#N/A。 (如果不是这样,请告诉我们!)

在以下两种情况下,

Application.Vlookup都会返回Error 2042

    lookup_value的第一列中找不到
  1. table_array(最常见的使用和期望,IMO)
  2. 找到
  3. lookup_value,但col_index_num的结果值本身就是错误。
  4. 因此,如果返回值可能包含错误,即使查找值存在,那么我们也不能使用Application.Vlookup来测试值的存在,但您可以使用替代方法,例如WorksheetFunction.CountIfApplication.Match

    在这里,我们只是查询列J并使用CountIf来确保至少有一个匹配值。这将提前验证我们的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会找到单元格的原因,但VLOOKUP不会,因为它需要完全匹配。

    看起来你正在构建/清理第一个循环中的查找表,你应该能够通过这样做来修复它:

    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