Consulting

Results 1 to 6 of 6

Thread: VlookUp giving random values instead of #N/A.

  1. #1
    VBAX Newbie
    Joined
    Nov 2017
    Posts
    4
    Location

    VlookUp giving random values instead of #N/A.

    I am using Vlookup to get values from another worksheet.
    If there are corresponsding values present, the vba vlookup workks fine.However, I want #N/A to be printed when there is no match but instead of that any random value from the existing values gets printed.
    Here is the code I am using:
    On Error Resume Next

    For i = 2 To 100

    Object = sheet1.Cells(i, "C").Value
    Range = sheet2.Range("C:I")

    Value1 = Application.WorksheetFunction.VLookup(Object, Range, 7, False)

    If IsError(Value1) Then

    Sheet1.Cells(i, "H") = "#N/A"

    Else

    Sheet1.Cells(i, "H") = Value1

    End If

    Next

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Also cross-posted and answered here: https://www.excelforum.com/excel-pro...ad-of-n-a.html
    Be as you wish to seem

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Use Find instead of a worksheet function
    Sub Test()
        On Error Resume Next
        For i = 2 To 100
            Object = Sheet1.Cells(i, "C").Value
            Set Rng = Sheet2.Range("C:C")
            Set Value1 = Rng.Find(Object, lookat:=xlWhole)
            If Value1 Is Nothing Then
                Sheet1.Cells(i, "H") = "#N/A"
            Else
                Sheet1.Cells(i, "H") = Value1.Offset(, 6)
            End If
        Next
    End Sub
    You can't use RANGE as a variable name and the function does not return a value if nothing is found: the previous value is retained.
    This should work, but I don't like it
    Sub Test2()
       On Error Resume Next
        For i = 2 To 100
            Object = Sheet1.Cells(i, "C").Value
            Set Rnge = Sheet2.Range("C:I")
            Value1 = "#N/A"
            Value1 = Application.WorksheetFunction.VLookup(Object, Rnge, 7, False)
            Sheet1.Cells(i, "H") = Value1
        Next
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    If you're going to use Find, you should really specify all its parameters, given that they persist between calls - including calls made from the UI. The code in the linked answer would work too.
    Be as you wish to seem

  5. #5
    VBAX Newbie
    Joined
    Nov 2017
    Posts
    4
    Location
    Quote Originally Posted by mdmackillop View Post
    Use Find instead of a worksheet function
    Sub Test()
        On Error Resume Next
        For i = 2 To 100
            Object = Sheet1.Cells(i, "C").Value
            Set Rng = Sheet2.Range("C:C")
            Set Value1 = Rng.Find(Object, lookat:=xlWhole)
            If Value1 Is Nothing Then
                Sheet1.Cells(i, "H") = "#N/A"
            Else
                Sheet1.Cells(i, "H") = Value1.Offset(, 6)
            End If
        Next
    End Sub

    Thanks Aflaton

    It worked by putting #N/A. I understand It's not a good practice but for the time being its good and please could you suggest me to make vlookup faster.

    there are more 2k rows and it's taking around 40 min to run

    You can't use RANGE as a variable name and the function does not return a value if nothing is found: the previous value is retained.
    This should work, but I don't like it
    Sub Test2()
       On Error Resume Next
        For i = 2 To 100
            Object = Sheet1.Cells(i, "C").Value
            Set Rnge = Sheet2.Range("C:I")
            Value1 = "#N/A"
            Value1 = Application.WorksheetFunction.VLookup(Object, Rnge, 7, False)
            Sheet1.Cells(i, "H") = Value1
        Next
    End Sub

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    It's unclear whom you are addressing in your response. Please include only specific lines in quotes pertinent to your response.
    If VLOOKUP is slow, don't use it. FIND can do the same job.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •