+ Reply to Thread
Results 1 to 7 of 7

Code for vlookup with changing ranges

  1. #1
    Registered User
    Join Date
    03-16-2012
    Location
    Wellington
    MS-Off Ver
    Excel 2010
    Posts
    20

    Code for vlookup with changing ranges

    Hi, I have been unable to find a vba code for vlookup that works for me. Would someone be able to help please. I would prefer a vba code solution if possible.

    As a function in a cell it looks like this: =VLOOKUP(A1,G1:K5,4,FALSE) (although G1:K5 is just an example it could go to K5000)
    I need it to work through all the rows of col A where the number of rows in col A will vary for each time the macro is run with new data.
    Cols G to K will all have the same number of rows as each other but this number will will vary for each time the macro is run with new data.

    thank you.

  2. #2
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Code for vlookup with changing ranges

    you could reference the entire column like this, it would work regardless of how large the column is.

    =VLOOKUP(A1,G:K,4,FALSE)
    If you liked my solution, please click on the Star -- to add to my reputation

    If your issue as been resolved, please clearly state so and mark the thread as [SOLVED] using the thread tools just above the first post.

  3. #3
    Registered User
    Join Date
    03-16-2012
    Location
    Wellington
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Code for vlookup with changing ranges

    Quote Originally Posted by DGagnon View Post
    you could reference the entire column like this, it would work regardless of how large the column is.

    =VLOOKUP(A1,G:K,4,FALSE)
    Thanks DGagnon but that I'm hoping for the full vba code because when I've tried writing some myself (with all hope and no knowledge) and use something along the lines of "application.worksheetfunctions.vlookup (A1,G:K, ..)" I get messages saying the colon in G:K is not acceptable.

  4. #4
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Code for vlookup with changing ranges

    do you want the vlookup to be in your cell, or are you trying to get the value that woudl be the result of the vlookup?

    could you also provide the surounding code?

  5. #5
    Registered User
    Join Date
    03-16-2012
    Location
    Wellington
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Code for vlookup with changing ranges

    Quote Originally Posted by DGagnon View Post
    do you want the vlookup to be in your cell, or are you trying to get the value that woudl be the result of the vlookup?

    could you also provide the surounding code?
    I want the value to be in the cell. I forgot to mention that these values will be put into col F. At this stage there is no other code - this is the first process done to this data. When the vlookup results are in place I will then be comparing the values in F with those in D (in the same row) . I hope that is a bit clearer. Thanks

  6. #6
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Code for vlookup with changing ranges

    you will need to include this in a loop im assuming, try something like this.

    Please Login or Register  to view this content.
    where i is your current row in your loop

  7. #7
    Registered User
    Join Date
    03-16-2012
    Location
    Wellington
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Code for vlookup with changing ranges

    Quote Originally Posted by DGagnon View Post
    you will need to include this in a loop im assuming, try something like this.

    Please Login or Register  to view this content.
    where i is your current row in your loop
    I'm embarrassed to say I can't get this running as I'm only new to vba. Would you please provide the entire code for running this as if you are writing it to a new work book and this is the only function that will be run.

+ 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