+ Reply to Thread
Results 1 to 14 of 14

function string to find non-overlapping ranges of values

  1. #1
    Registered User
    Join Date
    04-24-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    43

    function string to find non-overlapping ranges of values

    Dear all,

    I'm trying to implement a little look up routine in Excel (not in VBA) and can't quite seem to crack it.

    Essentially, I have rows of data showing min and max values in separate columns. Starting with the first row, I want to look down at each row and flag with an "x" those rows which have min and max values (i.e. ranges) which don't overlap with any other rows. The columns are sorted from min to max.

    For instance given the following data:

    0 10
    10 30
    10 35
    10 37
    20 40
    40 45
    40 47
    50 60

    The function I need would start with an x in row 1 and find all subsequent non-overlapping ranges, hence:

    0 10 x
    10 30 x
    10 35
    10 37
    20 40
    40 45 x
    40 47
    50 60 x

    The first range goes up to 10, so the function would then look for the next row which begins with 10 or more, which is the range in row 2, which is 10 to 30, etc..

    Any help would be gratefully appreciated, any questions just ask

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: function string to find non-overlapping ranges of values

    carbonboywonder,

    In cell C1 put an "x" because that is guaranteed not to overlap (as it it is the first in the dataset).
    Then in cell C2 and copied down, use this formula:
    =IF(B1>A2,"","x")
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    04-24-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: function string to find non-overlapping ranges of values

    Thanks for the reply tigeravatar,

    Apologies though - seems I used a poor example, sorry. In the dataset I have this won't always work. Consider these data instead:
    0 10 x
    10 30 x
    10 35
    10 37
    30 40 x
    40 45 x
    40 47
    50 60 x

    I changed the 20 to a 30, which makes things a little more difficult I think! The function needs to look back up the columns to find the last "x" I guess. I just don't quite know how this can be done without things getting circular. It's no problem to extend a solution to more columns if need be.

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: function string to find non-overlapping ranges of values

    carbonboywonder,

    In cell C2 and copied down:
    =IF(LOOKUP("x",$D$1:D1,$B$1:B1)>A2,"","x")

  5. #5
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,421

    Re: function string to find non-overlapping ranges of values

    I don't understand your logic. Doesn't the range 10 30 overlap the range 10 35 and the range 10 37 ?

    Pete

  6. #6
    Registered User
    Join Date
    04-24-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: function string to find non-overlapping ranges of values

    tigeravatar,
    Did you really mean $D$1:D1? Guessing you meant $C$1:C1 - which seems to have cracked it, thanks!

    Pete_UK. Yes, hence why these shouldn't be flagged. 10 30 is the first instance down the columns of a range which doesn't overlap with ranges in the rows above.
    Last edited by carbonboywonder; 05-01-2012 at 01:59 PM.

  7. #7
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,421

    Re: function string to find non-overlapping ranges of values

    Quote Originally Posted by carbonboywonder View Post
    Pete_UK. Yes, hence why these shouldn't be flagged. 10 30 is the first instance down the columns of a range which doesn't overlap with ranges in the rows above.
    I read this differently, then:

    Starting with the first row, I want to look down at each row and flag with an "x" those rows which have min and max values (i.e. ranges) which don't overlap with any other rows.
    Nothing there about flagging the first instance only.

    Just curious ...

    Pete

  8. #8
    Registered User
    Join Date
    04-24-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: function string to find non-overlapping ranges of values

    Ah yes, sorry for not being clear in the first instance!

  9. #9
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: function string to find non-overlapping ranges of values

    Quote Originally Posted by carbonboywonder View Post
    tigeravatar,
    Did you really mean $D$1:D1? Guessing you meant $C$1:C1 - which seems to have cracked it, thanks!

    Yeah, meant C instead of D, sorry about that. Glad to hear you got it working though

  10. #10
    Registered User
    Join Date
    04-24-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: function string to find non-overlapping ranges of values

    I've just noticed a slight issue with the function you gave me tigeravatar, sorry...

    I just extended my data by one row and got this:
    0 10 x
    10 30 x
    10 35
    10 37
    30 40 x
    40 45 x
    40 47
    50 60 x
    55 70 x

    There shouldn't be an x after the 55-70 row as this range overlaps with the 50-60 row above. If I change 55 to 44 it works, which suggests the lookup function isn't looking at the very first x above it but the first x it sees is the x next to the 40-45 row. Anyone any ideas why the lookup is doing this?

  11. #11
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: function string to find non-overlapping ranges of values

    carbonboywonder,

    Was hoping the formula wouldn't have to turn out like this because it is rather inefficient for large (more than 10000 rows) datasets. I had a feeling you would need it though. Anyways, here you go, this should work for you:

    =IF(LOOKUP(2,1/($C$1:C1="x"),$B$1:B1)>A2,"","x")

  12. #12
    Registered User
    Join Date
    04-24-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: function string to find non-overlapping ranges of values

    Thanks again tigeravatar, looks like this has done the trick. Without wishing to bother you even further, can you explain how this works. I'm familiar with Lookup but can't see how the 2,1/($C$1:C1="x") works? Feel free to leave it there though, this is a great help regardless :-)
    D

  13. #13
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: function string to find non-overlapping ranges of values

    lookup tries to find a match and returns a corresponding value. In the event that it cannot find a match, it returns the largest closest value that is still below the lookup value.

    So the lookup value is 2
    Then it generates an array of 1/(cells="x"). For each cell that = "x", the denominator becomes TRUE and for each cell that does not equal "x", the denominator becomes FALSE. Those boolean values are turned into their integer counterparts when a mathematical operator is performed on them. So 1/TRUE = 1/1 = 1 and 1/FALSE = 1/0 = #DIV/0! error.

    Using rows 1:7
    That array looks something like this when the cells are |"x"|"x"|""|""|"x"|"x"|""| : {1,1,#DIV/0!,#DIV/0!,1,1,#DIV/0!}
    So, none of those values are 2, which is the lookup value. So instead, lookup grabs the largest value that is still below 2, which in this case is 1. Lookup requires that data be sorted to work, and the formula will auto-sort the array. In the case of multiples of the same value, it will grab the last one generated. So in this case, the fourth 1.

    Then, lookup gets its corresponding value, what was in $B$1:B7. The fourth 1 that was generated was for row 6, so it grabs B6 = 45

    Now the formula is simply:
    If(45>50,"","x")
    45 is less than 50, so row 8 gets an "x".

  14. #14
    Registered User
    Join Date
    04-24-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    43

    Re: function string to find non-overlapping ranges of values

    Great stuff, thanks!

+ 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