+ Reply to Thread
Results 1 to 14 of 14

keeping col_index_num relative in a vlookup formula

  1. #1
    Registered User
    Join Date
    07-22-2010
    Location
    miami, fl
    MS-Off Ver
    Excel 2007
    Posts
    4

    keeping col_index_num relative in a vlookup formula

    I am using a simple vlookup formula in worksheet A to find values from worksheet B. in my formula, my table array contains absolute cell range (i.e. $A$6:$z$26). However, if I add a row in my worksheet B, then my results get screwed up because my col_index_num in my formula is now off by the number of rows I inserted into worksheet B. Is there a way to automatically change the col_index_num when I modify the data table the formula is pulling from?

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: keeping col_index_num relative in a vlookup formula

    Give your range on worksheet B a name by selecting the entire range and then typing a name into the Name box at the top left of the screen, just below the tool bar, e.g. SearchRange

    Then change your VLookup formula, replacing $A$6:$z$26 with SearchRange, or whatever you called it.

    Edited to add: Sorry, I just looked at your post again, and I'm a bit confused - you're asking about the column return reference, which shouldn't be affected by inserting rows.

    If you're actually inserting columns, which will make a difference, then simply naming the range won't solve the problem.

    The only way I can think of doing it via formula is to add a header row to your table (say in Row 4) and giving each column a header. Then, in the row below the header have a column reference, starting at 1 for the first column and incrementing by 1 for each column. I'd do this by just putting the formula =COLUMN() in each column.

    Then select the header row and the column reference row and give them a name, say HeaderRow

    So if, for example, column 1 was "Forename", column 2 "Surname", column 3 "Job" and so on, you'd write your VLOOKUP to say:

    =VLOOKUP(SearchRange,ValueToFind,HLOOKUP(HeaderRow,"Job",2,FALSE),FALSE)

    That should allow you to insert either rows or columns without having to change the VLOOKUP.
    Last edited by Andrew-R; 07-22-2010 at 11:46 AM.

  3. #3
    Registered User
    Join Date
    07-22-2010
    Location
    miami, fl
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: keeping col_index_num relative in a vlookup formula

    I tried it, but it did not seem to help. My problem is that now, instead of being $A$6:$z$26, it becomes $a$6:$z$30, and now instead of needing the content of the cell in the 15th row (i.e. col_index_num 15), I need the content of the cell in the 19th row.
    So my formula first reads VLOOKUP(a1,'worksheetB'!$a$6:$z$26,15) but when I add the rows to worksheet B, the formula automatically updates to VLOOKUP(a1,'worksheetB'!$a$6:$z$30,15) - even though the table array automatically updates, the index (15) does not automatically change to 19. Is there a way to make it do that?

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: keeping col_index_num relative in a vlookup formula

    Yes, the bit I added to my post will work, but it's a little bit of a faff on.

  5. #5
    Registered User
    Join Date
    07-22-2010
    Location
    miami, fl
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: keeping col_index_num relative in a vlookup formula

    by the way, sorry - I mean HLOOKUP - .....

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: keeping col_index_num relative in a vlookup formula

    Use INDEX and MATCH instead:
    INDEX($a$20:$z$20,,match(a1,'worksheetB'!$a$6:$z$6))
    Remember what the dormouse said
    Feed your head

  7. #7
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: keeping col_index_num relative in a vlookup formula

    Here's the formula in an example sheet.

    As you'll see inserting a column into the range on sheet 2 doesn't alter the result of the VLOOKUP in cell A2 on sheet 1.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    07-22-2010
    Location
    miami, fl
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: keeping col_index_num relative in a vlookup formula

    Can you elaborate a bit? How would index and match work?

  9. #9
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: keeping col_index_num relative in a vlookup formula

    Per your example, it matches A1 in worksheet B range A6:Z6 to get a column number, then returns the value from row 20 in that column.

  10. #10
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: keeping col_index_num relative in a vlookup formula

    I think the problem is that if a row is inserted then the row reference changes, so you can't use an absolute row (i.e. 20) to reference it.

    I've turned my last spreadsheet around to use HLOOKUP instead of VLOOKUP, and this will work for everything except a new row being inserted in Row 1.
    Attached Files Attached Files

  11. #11
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: keeping col_index_num relative in a vlookup formula

    If you insert a row above row 20, the formula will automatically adjust to use row 21.

  12. #12
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: keeping col_index_num relative in a vlookup formula

    Sorry, I'm being dense ... how does it know which row to return?

    If I was doing an HLOOKUP command I'd have HLOOKUP(search value, search range, row to return, T/F exact match)

    So where, in the INDEX and MATCH is the equivalent to the row to return?

    My apologies if I'm being thick here, but I've really never used INDEX and MATCH before.

  13. #13
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: keeping col_index_num relative in a vlookup formula

    Breaking the formula down:
    match(a1,'worksheetB'!$a$6:$z$6)
    returns a position where A1 is found in A6:Z6 on worksheetB. e.g. if it's found in D6, the MATCH formula will return position 4.
    The INDEX part then becomes:
    =INDEX($a$20:$z$20,,4)
    which means return the value in the fourth column of A20:Z20 - in other words the value in D20.
    Make sense?

    INDEX and MATCH is much more efficient than HLOOKUP and VLOOKUP generally because it only references the 2 rows or columns that you are actually interested in, not an entire table. It also has the benefit that if you want to return multiple values for the same lookup value, you can put the match formula in a separate column and refer to it directly in multiple INDEX formulas. I've attached a demo workbook.
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: keeping col_index_num relative in a vlookup formula

    Thanks for the explanation - I'll just have to find an excuse to use a MATCH/INDEX now.

+ 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