+ Reply to Thread
Results 1 to 11 of 11

Excel 2007 : Finding the Next Highest Value in an Array

  1. #1
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Finding the Next Highest Value in an Array

    I am using VLOOKUP with the not_exact_match set to True, however instead of finding the next largest value that is less than value, I want to find a way of returning the next largest value that is greater than value. I have looked at using MATCH and OFFSET to try and increment the returned value by 1, but so far haven't managed to figure this out.

    Can anyone suggest a way to achieve this.

    Many thanks

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Finding the Next Highest Value in an Array

    Perhaps apply LARGE

    =SMALL(range,1+COUNTIF(range,"<="&value))

  3. #3
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Finding the Next Highest Value in an Array

    Thanks DonkeyOte. I don't think this will work. It is conditional depending on whether VLOOKUP does or doesn't find an exact match.

    If it does find an exact match then it uses the value obtained by the VLOOKUP, however if it doesn't find an exact match I simply want a formula to get the value in the cell below, but it still needs to incorporate a VLOOKUP.

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Finding the Next Highest Value in an Array

    If a value does exist can it appear more than once ?

    If not a basic:

    =INDEX(<return range>,MATCH(<critera>,<criteria range>)+(LOOKUP(<criteria>,<criteria range>)<><criteria>))

    (assuming data is sorted - implied in first post)

    If in doubt - post a sample - covers half a dozen posts to & fro.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: Finding the Next Highest Value in an Array

    From the Help for VLOOKUP:

    If range_lookup is either TRUE or is omitted, an exact or approximate match is returned. If an exact match is not found, the next largest value that is less than lookup_value is returned.

    Important: If range_lookup is either TRUE or is omitted, the values in the first column of table_array must be placed in ascending sort order; otherwise, VLOOKUP might not return the correct value.


    From the Help for Match:

    1 or omitted MATCH finds the largest value that is less than or equal to lookup_value. The values in the lookup_array argument must be placed in ascending order, for example: ...-2, -1, 0, 1, 2, ..., A-Z, FALSE, TRUE.

    0 MATCH finds the first value that is exactly equal to lookup_value. The values in the lookup_array argument can be in any order.

    -1 MATCH finds the smallest value that is greater than or equal to lookup_value. The values in the lookup_array argument must be placed in descending order, for example: TRUE, FALSE, Z-A, ...2, 1, 0, -1, -2, ..., and so on.


    So, you can use INDEX/MATCH but your data must be sorted in descending order.

    =INDEX(A:A,MATCH(C1,A:A,-1))


    Regards

  6. #6
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Finding the Next Highest Value in an Array

    Thanks DonkeyOte and TMShucks for your replies.

    I think I may need to make my problem a bit cleaerer. I have attached a simple example to hopefully demonstrate what I want to achieve.

    The givens are that the data is (and has to remain) sorted in ascending order by date (Column A).

    I have a named ranged called Table in A2:B21. In Column D, I have specified some dates to lookup in my named range and in Column F, I have detailed the results of the lookup, using the formula =VLOOKUP(Cell Ref,Table,2,TRUE). The VLOOKUP is looking for the date from Column D within my named range and returning the value for Interest Accrued from Column B.

    When an exact match is found, I get the correct result, however when an exact match isn't found, I get the next largest value that is less than value (as expected), so when it is looking for 31-Jan-09 it returns the value for 31-Dec-08.

    What I want though is a way of finding the next largest value that is greater than value. So when looking for 31-Jan-09 instead of returning the value for 31-Dec-09, I want it to return the value for 31-Mar-09. The same applies when looking for 28-Feb-09, again I want a formula that will return 31-Mar-09, i.e. the next largest value that is greater than value, rather than the next largest value that is less than value.

    This is where I am stuck.
    Attached Files Attached Files
    Last edited by HangMan; 09-13-2010 at 03:35 AM.

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Finding the Next Highest Value in an Array

    Per the sample and use of Named Range

    Please Login or Register  to view this content.
    this is pretty much the same as my prior post just with range references filled in as appropriate

    And adapting the COUNTIF concept:

    Please Login or Register  to view this content.
    though shorter the above is less efficient than the first example
    Last edited by DonkeyOte; 09-13-2010 at 04:11 AM.

  8. #8
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Finding the Next Highest Value in an Array

    Hey DonkeyOte

    Many thanks, both these solutions worked perfectly. Very much appreciated.

    Out of interest an just so I understand, what makes the second approach less efficient?

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Finding the Next Highest Value in an Array

    The first approach uses two Binary Search based functions and these are very fast. Binary Searches use only a very small subset of the precedent range.
    (for a graphical representation of the basics see: http://www.mrexcel.com/forum/showpos...98&postcount=7)

    The second approach though shorter in terms of syntax uses a COUNTIF statement - this means each cell in the precedent range (within the intersect of the used range) is processed.

    In truth, given COUNTIF is itself pretty efficient, the difference in performance will be negligible on anything but immense data sets and/or where COUNTIF used in large volume.

  10. #10
    Registered User
    Join Date
    09-01-2011
    Location
    Denver, USA
    MS-Off Ver
    Excel 20010
    Posts
    4

    Re: Excel 2007 : Finding the Next Highest Value in an Array

    Sometimes for legibility or complicated calculations you do not what to include the search array more than once. Here is a trick to fool vlookup or hlookup to give you the next highest instead of next lowest number in a sorted list.

    for example hlookup(2.5,{1,2,3,4},1) = 2, but you wanted 3

    so you negate the result, value, array, and resort to get this result:

    -hlookup(-2.5,{-4,-3,-2,-1},1) = 3

  11. #11
    Registered User
    Join Date
    01-28-2018
    Location
    Dhaka, Bangladesh
    MS-Off Ver
    MS 10
    Posts
    1

    Re: Finding the Next Highest Value in an Array

    @DonkeyOte
    Excellent sir!!!! Just brilliant!! It helped me a lot. I just registered to this site only for giving you a big Thank you.
    regards
    Rabiul Alam
    Writing from Bangladesh.

+ 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