+ Reply to Thread
Results 1 to 13 of 13

VBA V Lookup

  1. #1
    Forum Contributor
    Join Date
    09-21-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    263

    VBA V Lookup

    Hello All!!

    Ok - The below code is a V Lookup written in VBA. It works perfectly, other than, when the lookup table does not have the exact value, it picks a value from the lookup table and fills in the corresponding cell.

    Example: If "606" = "Mary" in the lookup table, and the lookup came across "505" it may still fill in the corresponding cell with "Mary" with no apparent reason or logic of what it chooses. I should provide a NA error or something other than a random value picked from the lookup table. Hope this makes sense. Thanks to anyone who can help identify this issue.


    Please Login or Register  to view this content.
    Last edited by Lonney; 10-25-2014 at 06:30 PM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,384

    Re: VBA V Lookup

    Try:

    Please Login or Register  to view this content.

    Then, if the VLOOKUP fails, you should get NA.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Contributor
    Join Date
    09-21-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    263

    Re: VBA V Lookup

    Thanks TMS, but it still is not working. Odd. I cant figure it out.

  4. #4
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA V Lookup

    Try

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  5. #5
    Forum Contributor
    Join Date
    09-21-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    263

    Re: VBA V Lookup

    Thanks Mikerickson - I am sure this is user error, I am getting the following error: Application-defined or object-defined error.

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    09-21-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    263

    Re: VBA V Lookup

    The below code runs, but does not place any value in the cells.

    Please Login or Register  to view this content.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA V Lookup

    comment out the .Value = .Value line and see what formula its putting in those cells.

  8. #8
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,602

    Re: VBA V Lookup

    You are processing with two sheets in the code: "CallData" & Sheet16. But you have:
    With Range("N2:N" & LR) which will operate on the currently selected sheet.
    You should have:
    With Sheet("x").Range("N2:N" & LR)...

    Also, I would suggest not using the sheet's code name (Sheet16), but the tab name instead; since the code name can change depending on sheet additions/deletions. The tab name won't .
    Ben Van Johnson

  9. #9
    Forum Contributor
    Join Date
    09-21-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    263

    Re: VBA V Lookup

    There are no values being entered into those cells....UGH

  10. #10
    Forum Contributor
    Join Date
    09-21-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    263

    Re: VBA V Lookup

    Ahh - Let me try that. That may be the issue

  11. #11
    Forum Contributor
    Join Date
    09-21-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    263

    Re: VBA V Lookup

    Ok - I updated the code as noted below, but now have the following error: Object variable or With block variable not set. I think it was getting confused with the different spreadsheets.

    Please Login or Register  to view this content.

  12. #12
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA V Lookup

    Eliminate the For loop, use the keyword Set and insert some S's
    Please Login or Register  to view this content.
    That was probably your issue all along, since vLookupTable is a Variant, and you weren't using the Set keyword, you were getting a variant array, and VLOOKUP is happier with ranges than variant arrays. Declaring it as Range isn't necessary (but it does save resources). The Set is needed to assign a Range object to a variable.
    Last edited by mikerickson; 10-25-2014 at 11:18 PM.

  13. #13
    Forum Contributor
    Join Date
    09-21-2014
    Location
    USA
    MS-Off Ver
    2013
    Posts
    263

    Re: VBA V Lookup

    Works PERFECT!!!!! Thank-you all!!!!!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 04-08-2014, 03:11 AM
  2. Replies: 2
    Last Post: 05-19-2013, 08:46 AM
  3. Replies: 1
    Last Post: 11-07-2012, 01:57 PM
  4. Search lookup array to find lookup value contained within text string
    By Cookstein2 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-02-2011, 09:42 AM
  5. Replies: 7
    Last Post: 06-19-2011, 12:51 PM

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