+ Reply to Thread
Results 1 to 10 of 10

Range for Vlookup: refer to columns stored in a variable

  1. #1
    Registered User
    Join Date
    07-09-2012
    Location
    netherlands
    MS-Off Ver
    Excel 2010
    Posts
    5

    Range for Vlookup: refer to columns stored in a variable

    Hi,
    I’m new in the VBA world and in this forum. I’m trying to do a vlookup but I’m facing a problem
    I need to refer to a range in another worksheet. The columns to which I want to refer can vary position. Therefore I get their column number and store it in a variable (ColC, ColN)
    I don’t know how to refer to the range. If I refer to the range by the letter of the columns, the vlookup works.

    Please Login or Register  to view this content.
    However these columns can move and I need to find a way to refer to this range without using (“B:E”).
    I’ve tried this but it doesn’t work

    Please Login or Register  to view this content.
    Any suggestion? Thanks a lot for the help!
    Last edited by Cutter; 07-10-2012 at 04:28 PM. Reason: Added code tags

  2. #2
    Forum Contributor
    Join Date
    07-05-2012
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Range for Vlookup: refer to columns stored in a variable

    Try creating a string variable and concatenating the two other variables, then using the created string in the VLookUp:

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-09-2012
    Location
    netherlands
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Range for Vlookup: refer to columns stored in a variable

    Hi,
    thanks for the help. It's still not working.
    Any other suggestion?

  4. #4
    Forum Contributor
    Join Date
    07-05-2012
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Range for Vlookup: refer to columns stored in a variable

    Can you submit a sample of the workbook with fake data so I can manipulate it?

  5. #5
    Registered User
    Join Date
    07-09-2012
    Location
    netherlands
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Range for Vlookup: refer to columns stored in a variable

    Hi,
    this is an example. Thanks
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    07-05-2012
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Range for Vlookup: refer to columns stored in a variable

    Try this:

    Please Login or Register  to view this content.
    Last edited by Xer2; 07-10-2012 at 04:31 PM. Reason: Updated Code

  7. #7
    Registered User
    Join Date
    07-09-2012
    Location
    netherlands
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Range for Vlookup: refer to columns stored in a variable

    Thanks for the help,
    but what I'm trying to achieve is not to use explicitly columns "B:E" in the Vlookup Range.
    These columns can change and this is why I'm storing the colum number in the variable ColC and ColN.

    you wrote this, but in thi B and E are used

    Please Login or Register  to view this content.
    I'm tring to define the range for the Vlookup by using ColC and ColN; but if I write this it will not work

    Please Login or Register  to view this content.
    I've also tried

    Please Login or Register  to view this content.
    Last edited by stefy1984; 07-10-2012 at 04:52 PM.

  8. #8
    Forum Contributor
    Join Date
    07-05-2012
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Range for Vlookup: refer to columns stored in a variable

    Try the code in this. It is long and not good looking, but it works.
    Attached Files Attached Files
    Last edited by Xer2; 07-10-2012 at 05:49 PM.

  9. #9
    Registered User
    Join Date
    07-09-2012
    Location
    netherlands
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Range for Vlookup: refer to columns stored in a variable

    Thanks a lot for the help.

    I also kept trying and I think I found my mistake.
    This seems to work

    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    07-05-2012
    Location
    Houston, Texas
    MS-Off Ver
    Excel 2016
    Posts
    165

    Re: Range for Vlookup: refer to columns stored in a variable

    Oh yeah, looks much better!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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