+ Reply to Thread
Results 1 to 10 of 10

VlookUp function

  1. #1
    Registered User
    Join Date
    04-04-2012
    Location
    france
    MS-Off Ver
    Excel 2003
    Posts
    14

    Question VlookUp function

    Hi,

    When I used a Vlookup function with VBA, I've got #N/A results, does any body knox why?

    The code that I used is :

    Please Login or Register  to view this content.
    Last edited by arlu1201; 04-12-2012 at 07:44 AM. Reason: Please put code tags in future.

  2. #2
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470

    Re: VlookUp function

    Quote Originally Posted by yalmallo View Post
    Hi,

    When I used a Vlookup function with VBA, I've got #N/A results, does any body knox why?
    The code that I used is :

    Range("Q2", Range("Q2").End(xlDown)) = Application.VLookup(Range("F2", Range("F2").End(xlDown)), Range("$H$2:$K$2", Range("$H$2:$K$2").End(xlDown)), 3, False)
    Change to
    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.
    Best regard, -)iger-/iger
    If you are pleased with a solution mark your post SOLVED.

  3. #3
    Valued Forum Contributor
    Join Date
    12-05-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010 & 2013
    Posts
    308

    Re: VlookUp function

    Hi yalmallo

    Generally, #NA means the value doesn't exist in the list. Can you upload a sample sheet so we can see what the code is interacting with? Without this it is difficult to see why the code won't work.

    In the meantime - things to check:
    - I think the first value in the Vlookup command is incorrect. Typical VLookup command: VLOOKUP(F2, H2:K7,3,False). You have a multi-cell range as the first argument. Your vlookup looks like: VLOOKUP(F2:F7, H2:K7,3,False).
    - Does the range contain the value you are looking for? Beware of rogue spaces, etc.
    - Is the lookup table range being correctly selected? Are there any blank rows/cells that could interrupt the End(xlDown)?

    Cheers, Rob.

  4. #4
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: VlookUp function

    If there is nothing on the Q column when you run this, you will get a sequence of #N/As after the valid results. Is that what you are seeing? Since you want the update for the Q column to stop at the end of the F column you might try this
    Please Login or Register  to view this content.
    Bob
    Click my star if my answer helped you. Mark the thread as [SOLVED] if it has been.

  5. #5
    Registered User
    Join Date
    04-04-2012
    Location
    france
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: VlookUp function

    Quote Originally Posted by rscsmith View Post
    Hi yalmallo

    Generally, #NA means the value doesn't exist in the list. Can you upload a sample sheet so we can see what the code is interacting with? Without this it is difficult to see why the code won't work.

    In the meantime - things to check:
    - I think the first value in the Vlookup command is incorrect. Typical VLookup command: VLOOKUP(F2, H2:K7,3,False). You have a multi-cell range as the first argument. Your vlookup looks like: VLOOKUP(F2:F7, H2:K7,3,False).
    - Does the range contain the value you are looking for? Beware of rogue spaces, etc.
    - Is the lookup table range being correctly selected? Are there any blank rows/cells that could interrupt the End(xlDown)?

    Cheers, Rob.


    I think that the problem is that I have some empty cells in the xls file. So, VLOOKUP could not support empty cells. So I think That I have to do a éLOOPé or something like this!!!

  6. #6
    Registered User
    Join Date
    04-04-2012
    Location
    france
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: VlookUp function

    Quote Originally Posted by blane245 View Post
    If there is nothing on the Q column when you run this, you will get a sequence of #N/As after the valid results. Is that what you are seeing? Since you want the update for the Q column to stop at the end of the F column you might try this
    Please Login or Register  to view this content.

    this code gives lastrow = 3. and it has to have a value of 4505 at least!

  7. #7
    Registered User
    Join Date
    04-04-2012
    Location
    france
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: VlookUp function

    Quote Originally Posted by tigertiger View Post
    Change to
    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.

    the first code takes a lot of time and it doesn't stop.

    the second one doesn't work!!!

  8. #8
    Registered User
    Join Date
    04-04-2012
    Location
    france
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: VlookUp function

    This one works:


    Dim lastRow As Long
    lastRow = Range("D2").End(xlDown).Row
    Range("Q2:Q" & lastRow) = _
    Application.VLookup(Range("F2:F" & lastRow), _
    Range("$H$2:$K$2", Range("$H$2:$K$2").End(xlDown)), 3, False)




    I've juste changed the column of the lastrow to be D2. And it works.

    I think that the problem becomes from that the column F2 has the fourth cell empty, VBA stops here and returns lastrow=3.

  9. #9
    Valued Forum Contributor blane245's Avatar
    Join Date
    02-20-2009
    Location
    Melbourne, FL
    MS-Off Ver
    Excel 2010
    Posts
    649

    Re: VlookUp function

    Yes, empty cells definately mess up End(xlDown). You might try the UsedRange property. It is a bit more reliable.

  10. #10
    Registered User
    Join Date
    04-04-2012
    Location
    france
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: VlookUp function

    YES.


    Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1