+ Reply to Thread
Results 1 to 13 of 13

Macro Vlookup does not recognizes named range

  1. #1
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Macro Vlookup does not recognizes named range

    Hi, the code below returns error 1004. And when I replace "myRange" at the vlookup line with "a1:g4" it works. What's wrong with my code?
    Please Login or Register  to view this content.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Macro Vlookup does not recognizes named range

    In that case it would be a declared variable, not a named range. Try:
    Please Login or Register  to view this content.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Macro Vlookup does not recognizes named range

    Hi, No I get this error again..I add now the full code, and hope that till tommorrow something will pop up. thanks
    Please Login or Register  to view this content.
    Last edited by GIS2013; 08-26-2013 at 06:12 PM. Reason: typo

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Macro Vlookup does not recognizes named range

    Where are you getting the error and what kind? You have undeclared variables.

    Also I'm not that strong with Vlook ups but doesn't the first part need to be a value? So:
    Please Login or Register  to view this content.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Macro Vlookup does not recognizes named range

    You don't need Range.
    Please Login or Register  to view this content.
    By the way, what are you actually looking up?

    The lookup range you is 7 columns wide but you are only returning the first (A) column.
    If posting code please use code tags, see here.

  6. #6
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Macro Vlookup does not recognizes named range

    hi GIS2013, you are using 1 column for the result to come from Vlookup. That would give you an error. Change the column number in Vlookup or use different set of functions (Match, INDEX) if you want to return value on the left from the searched column

  7. #7
    Valued Forum Contributor
    Join Date
    07-17-2005
    Location
    Abergavenny, Wales, UK
    MS-Off Ver
    XL2003, XL2007, XL2010, XL2013, XL2016
    Posts
    608

    Re: Macro Vlookup does not recognizes named range

    Hi

    Without seeing your data it is difficult to be sure, but
    Sheets("1").Activate is not the same sheet you refer to for lr = Sheets(1).UsedRange.Rows.Count

    and

    Result = Application.VLookup(Range("J4"), Range(myRange), 1, False)

    should be

    Result = Application.VLookup(Range("J4"), myRange, 1, False)
    --
    Regards
    Roger Govier
    Microsoft Excel MVP

  8. #8
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Macro Vlookup does not recognizes named range

    Thank you all. @norie - to your question, trying to build a search form. But it's more complicated than I though. I was thinking that a user would type "Bob", for example, in a textbox in the userform, and then click a button that runs vlookup search throughout the db and gets back the rest of the data regarding that name (if item = 1 to 7, e.g. 7 items per person, and the user looks for the i item, he'll get back the other 7-i items..)
    Last edited by GIS2013; 08-27-2013 at 09:44 AM. Reason: typo

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Macro Vlookup does not recognizes named range

    You don't need Vlookup for that, use Match instead.

    Match will return the row number that the user input, eg Bob, is entered and from that you can return the rest of the data for 'Bob', or whoever.

    Let's say TextBox1 is where the user enters the name and the data is returned to TextBox2-9.
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Macro Vlookup does not recognizes named range

    @norie, just a question about the "me" you put twice along the code. What does it do? the code is not running because of this..

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Macro Vlookup does not recognizes named range

    Me refers to the userform the code is in.

    It shouldn't stop the code running, unless of course the code isn't in the userform module.

    Perhaps you could upload a sample workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  12. #12
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Macro Vlookup does not recognizes named range

    It really wasn't but now it is. And now no error, but I still dno't get results. Very weird the code looks perfect. Note I've changed from textbox1 to textbox15 (but same idea - the box the user enters what he's looking for), and the For loop runs from textboxed 16 to 17, but also the same idea as you meant..

  13. #13
    Forum Contributor
    Join Date
    12-30-2012
    Location
    Israel
    MS-Off Ver
    2007, 2016
    Posts
    642

    Re: Macro Vlookup does not recognizes named range

    @norie, I've just finished making a sample file, then I run it (the sample file) and wonder of wonders it's working. Now it's my job to figuring out what fails my real db. Really appreciate your help
    Last edited by GIS2013; 08-27-2013 at 11:12 AM. Reason: typo

+ 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. [SOLVED] IF Function that recognizes a range
    By missmea2005 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 01-11-2013, 08:26 AM
  2. Vlookup with named range
    By realniceguy5000 in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-23-2011, 02:36 PM
  3. Replies: 3
    Last Post: 06-04-2011, 10:56 AM
  4. Using Named range in a Vlookup
    By bernie.kruger in forum Excel General
    Replies: 7
    Last Post: 11-03-2010, 09:22 AM
  5. Insert Vlookup into table_array of Vlookup with named range
    By Denise in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-24-2005, 07:06 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