+ Reply to Thread
Results 1 to 14 of 14

A lookup function to search through array and return row number

  1. #1
    Registered User
    Join Date
    02-08-2010
    Location
    Madison, Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    47

    A lookup function to search through array and return row number

    Hi. I am working with essentially the data below:

    A B C D E

    21 - #1 2008 2012 2016 2020
    22 - #2 2009 2013 2017 2021
    23 - #3 2010 2014 2018 2022
    24 - #4 2011 2015 2019 2023


    I am trying to get a formula that will search for the year in A5 in the above array and return a result of the # in column A corresponding to the row of the year.

    HLOOKUP(A5,A21:E24,1,FALSE) is what I've been trying, but I get an N/A response. Am I confusing the H and V LOOKUP functions? Or do have the wrong function altogether?

    Thanks!!

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: A lookup function

    Try:

    =INDEX($A$21:$A$24,SUMPRODUCT(($B$21:$E$24=A5)*(ROW($B$21:$E$24)-ROW($B$21)+1)))
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: A lookup function to search through array and return row number

    Just asking for clarification on something. Are you really wanting a LOOKUP to find the number which could be any value (not simply 1-4 as shown in your example) or do you just want to know which number from 1 to 4 is in column A of your array?

    If the latter then you can just use:

    =MOD(A5,4)+1

    which doesn't need to reference the array at all.

    If the former then I think you can simply use:

    =INDEX(A21:A24,MOD(A5,4)+1)
    Last edited by Cutter; 12-03-2010 at 02:50 PM.

  4. #4
    Registered User
    Join Date
    02-08-2010
    Location
    Madison, Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: A lookup function to search through array and return row number

    NBVC - I'm not sure what's happening, but I always get #1.

    Cutter - I am trying to see which row the year in A5 is in and then return the column A description for that row (#1, #2, #3, or #4). I hope that makes sense. So I'm not sure which of your suggestions fits that, if any.

    Thanks guys for your help. I was apparently wrong in thinking an easy lookup formula would work.

  5. #5
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: A lookup function to search through array and return row number

    See and test the attached.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    02-08-2010
    Location
    Madison, Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: A lookup function to search through array and return row number

    NBVC - Thanks that works now. But when I copy and paste to the next worksheet, I get a VALUE error. Everything is the exact same as far as I can tell, including formatting. After doing the control+C for the cell, I copied the formula directly in the formulas bar and tried copying that. Same error. Any ideas? Thanks!

  7. #7
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: A lookup function to search through array and return row number

    Are you sure that the matches to be found are exact? Are you matching numbers to number? And if so are they all formatted properly as numbers?

  8. #8
    Registered User
    Join Date
    02-08-2010
    Location
    Madison, Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: A lookup function to search through array and return row number

    I think the matches are to be exact. The dates will be either 2010, 2011, 2012 etc etc. My actual array extends further than the example - through 2051. I should be covered for a while. I need to aske if the year in A5 matches a year in one of the rows (which it will) and then give me the corresponding value in the first column of that same row. To me that's an exact match, but maybe we're talking different languages here. Excel has my head spinning lately.

    The date in A5 is actually in a this format dddd,mmm,dd,yyyy and I'm using YEAR($A$5) in the formula. All the years in the array are formatted as numbers. I typed in the first year in each row and then used a B21+4 formula etc to get the next numbers. Is that screwing something up?

    Thanks for trying to help me work this through!

  9. #9
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: A lookup function to search through array and return row number

    The formulas shouldn't affect it. I am having a hard time picturing what could be different, if it is different from your original sheet.

    Can you post a more accurate worksheet.. just remove confidential data around the main parts.

  10. #10
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: A lookup function to search through array and return row number

    I take it the formulas I gave do not work???

  11. #11
    Registered User
    Join Date
    02-08-2010
    Location
    Madison, Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: A lookup function to search through array and return row number

    Cutter - your suggestions weren't working either. The first sheet was fine, but then I got some weird answers.

    NBVC, attached is a pared down sheet.

    So, in O4 and P4, I am looking to test the year of the date in A5 to see which of the years of the election cycle it is in based on the tab Cycles and the rows 20-23. So 2010 should be found in Row 22 and return the word midterm in O4. AS you can see, it works in the 2010 tab, but when I copy it to 2011 it doesn't work. But then it works again in 2012. In the full sheet I have 3 tabs that don't work and 7 that do. If you can see what the difference is, I'd love to know.

    Thanks again!!
    Attached Files Attached Files

  12. #12
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: A lookup function to search through array and return row number

    This works as far as I can see:

    =INDEX(CYCLES!$C$20:$C$23,MOD(YEAR(A5),4)+1)


    you have changed the rows from the Original Post. Maybe you didn't adjust what was given to you?

    Your OP also suggested just a YEAR in A5 but it is actually a DATE in A5 so that would screw up what I gave earlier.

    Here's one to make the CYCLES reference unnecessary:

    =CHOOSE(MOD(YEAR(A5),4)+1,"Election","Post-Election","Mid-Term","Pre-Election")
    Last edited by Cutter; 12-03-2010 at 07:07 PM.

  13. #13
    Registered User
    Join Date
    02-08-2010
    Location
    Madison, Wisconsin
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: A lookup function to search through array and return row number

    NBVC - closing and re-opening the spreadsheet fixed the problem.

    Cutter - thanks, that works, too.

    I guess NBVC is right - where there is a will, there are many ways.

    Thanks guys!!!!

  14. #14
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: A lookup function to search through array and return row number

    I'm just going to send what I did for you. I made some changes in columns H-K that will definitely simplify your file.

    Numerous formulas were unnecessary while the rest were over-complicated.
    Attached Files Attached Files
    Last edited by Cutter; 12-03-2010 at 10:54 PM.

+ 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