简体   繁体   中英

VBA Vlookup not finding match, Error 2042

I have been trying to work on this for a day or two. With some basic debugging I figured out it is not finding the referenced value, but I cant figure out why. The only thing I can think is I am not referencing the lookup table correctly, but everyway I try to reference it, it returns the same value.

I have 2 worksheets, "Database" and "Details". Database has a table called "Client_Function" with all the raw data in it, Details has a pivot table with limited data from the table (name and category). I want to show the extended data for a single item in the details worksheet on selection of a cell in the pivot table with a vlookup of the name. Since the pivot table consists of data directly pulled from the table, I know the lookup values exist. Here is the code I have.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Column = 1 And Target.Row > 4 Then
        On Error Resume Next
        Range("C2") = Application.WorksheetFunction.VLookup(Target.Text, Worksheets("Database").Range("Client_Function"), 2, 0)
    
        If Err.Number <> 0 Then
            MsgBox Err.Number & " " & Err.Description
        End If
    Else
        Range("C2") = ""
    End If

End Sub

I have tried Target.Text and Target.Value (both seem to return the correct value while debugging). I've tried storing the lookup value(Target.Text & Target.Value) into a variable before and then using that variable as the lookup value. I have also tried different ways of referencing the table in the Database Sheet, but nothing seems to work. I have also tried storing the return value of the function into a variable instead of directly into the cell.

I was wondering if the pivot table could be causing some issues, although I don't think so since the lookup value is correct.

2042 is the #N/A error, which means the value cannot be found. Note that the lookup is performed in the first column of the lookup table!!

Strategies to resolve this:

  • use VBA to write the target.Text into a worksheet cell.
  • then manually construct a Vlookup with this cell as the lookup
  • troubleshoot that formula in the workbook until you get the expected result.

The only reason for the #N/A error is that the lookup value cannot be found. The most common reasons for that are leading/trailing spaces and text instead of numbers.

If that target.Text comes from a pivot table, you may want to check that it doesn't contain leading or trailing spaces, used by the pivot table to indent things.

Also check that the lookup value and the first column of the lookup table are the same data type. If your data.table contains numbers in the first column of the range Client_Function, then the pivot table may have converted that to text. The target.Text will definitely return text, not a number, so you may want to convert the value back to a number before performing the lookup.

Finally, if your lookup table has the lookup value in a column other than the first, you may want to use Xlookup instead of Vlookup and specify the lookup column.

Since my data was going to be in a different column I actually just switched to an INDEX MATCH call.

Something like:

Range("C2") = WorksheetFunction.Index(Worksheets("Database").Range("Client_Function[Version]"), WorksheetFunction.Match(Target.Text, Worksheets("Database").Range("Client_Function[Predefined Call]"), 0))

It is working properly now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM