+ Reply to Thread
Results 1 to 17 of 17

Count number of contiguous values in a range which meet conditions

  1. #1
    Registered User
    Join Date
    05-30-2014
    Posts
    11

    Count number of contiguous values in a range which meet conditions

    I'm wracking my brain trying to write a formula to count the number of values in a range which meet a certain set of conditions.

    The data are a list of numbers ranging from 0 - 1. I need to count the number of contiguous data points in each range which exceed a threshold value. This range must also include the maximum value in the data set (which is always the values of "1.0")

    Ive attached an example sheet showing the dataset (I have many more datasets to do this on! hence I'm trying to write a formula to automate it). in the example sheet Ive highlighted the cells which contain values which exceed the threshold of "0.5". Looking at dataset number 17 highlights the problem as there are a total of 10 values which exceed the threshold of 0.5, however, the correct result to return is "5" as there are only a maximum of 5 contiguous values which exceed the threshold and for which the range includes the value of 1.0.

    Any and all comments/suggestions are gratefully received
    Attached Files Attached Files

  2. #2
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Count number of contiguous values in a range which meet conditions

    Are you sure your results are correct?

    If the threshold is N then you want the longest "streak" of consecutive cells that are >N and <=1, correct?

    Here are the results I get for the first 3 data sets:

    Data Range
    AB
    AC
    AD
    10
    6
    6
    6
    11
    11
    9
    9
    12
    10
    3
    3


    Here are your results for the first 3 data sets:

    Data Range
    X
    Y
    Z
    10
    6
    6
    6
    11
    11
    10
    9
    12
    11
    4
    3
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  3. #3
    Registered User
    Join Date
    05-30-2014
    Posts
    11

    Re: Count number of contiguous values in a range which meet conditions

    Apologies, Let me go through the example sheet and check for any errors as a result of my ineptitude. I appreciate the help so far. I'll post back tomorrow.

  4. #4
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Count number of contiguous values in a range which meet conditions

    I used in X10 copied down, this Arrayed formula

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    ...confirmed by pressing CTRL+SHIFT+ENTER to activate the array, not just ENTER. You will know the array is active when you see curly braces { } appear around your formula. If you do not CTRL+SHIFT+ENTER you will get an error or a clearly incorrect answer. Press F2 on that cell and try again.

    See attachment
    Attached Files Attached Files
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

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

  5. #5
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Count number of contiguous values in a range which meet conditions

    That is cool....
    ChemistB
    Nice Job...
    Regards,
    Vikas Gautam
    Excel-buzz.blogspot.com

    Excel is not a matter of Experience, its a matter of Application.

    Say Thanks, Click * Add Reputation

  6. #6
    Registered User
    Join Date
    05-30-2014
    Posts
    11

    Re: Count number of contiguous values in a range which meet conditions

    There were some small errors in the results table I had provided. Apologies, they were present due to human error as a result of manually counting the values..... hence I needed the function!

    Many thanks ChemistB your function works perfectly!

  7. #7
    Registered User
    Join Date
    05-30-2014
    Posts
    11

    Re: Count number of contiguous values in a range which meet conditions

    Woops, may have been too hasty! I think there is a problem with the function which is illustrated below:

    For dataset #20 with a threshold value of "0.5" the correct result is a value of "4", not "5" as returned by your function. In the example sheet we can see that cells U5:U8 equal or exceed the threshold "0.5" and the range includes the value "1". There is a second range in this dataset which exceeds the threshold value of "0.5" (U15:U19), however, this range does not include the value "1" and so this range is ineligible. The correct range is U5:U8, therefore the correct result is a value of 4.

    Do you think you can add in a function to ensure the range includes the value "1"?

  8. #8
    Registered User
    Join Date
    05-30-2014
    Posts
    11

    Re: Count number of contiguous values in a range which meet conditions

    Woops, may have been too hasty! I think there is a problem with the function which is illustrated below:

    For dataset #20 (in the example sheet) with a threshold value of "0.5" the correct result is a value of "4", not "5" as returned by your function. In the example sheet we can see that cells U5:U8 equal or exceed the threshold "0.5" and the range includes the value "1". There is a second range in this dataset which exceeds the threshold value of "0.5" (U15:U19), however, this range does not include the value "1" and so this range is ineligible. The correct range is U5:U8, therefore the correct result is a value of 4.

    Do you think you can add in a function to ensure the range includes the value "1"?

  9. #9
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Count number of contiguous values in a range which meet conditions

    Hmm, struggling with this one. Tony, do you have a solution?

  10. #10
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Count number of contiguous values in a range which meet conditions

    I'm not sure how you'd do that.
    Last edited by Tony Valko; 09-14-2014 at 08:53 PM. Reason: added missing word

  11. #11
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Thumbs up Re: Count number of contiguous values in a range which meet conditions

    Hi ChemistB, Tony and Laughing..
    It took me 4 hours at night to get the job done with a Excel formulas.. I got near but wasn't perfect..
    So I thought of a UDF that will make the Calculations and lengths better..
    I have got the job done... in about 1 hour of hard work.. with UDF..
    Moreover it include all kind of possibilities of position of 1...
    So here is that..

    Please Login or Register  to view this content.
    Check the attached file...
    Attached Files Attached Files
    Last edited by Vikas_Gautam; 09-14-2014 at 01:08 AM.

  12. #12
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: Count number of contiguous values in a range which meet conditions

    Quote Originally Posted by Vikas_Gautam View Post
    I will request all of you to appreciate, if it is what you were trying to achieve..
    You don't need to request to appreciate your good work. The beauty of this forum is, here all the Forum Gurus, Mods and senior members always appreciate good work by any member.

    Well done. This is really good work indeed. You have provided OP another option with UDF to achieve the desired result.
    Regards
    sktneer


    Treat people the way you want to be treated. Talk to people the way you want to be talked to.
    Respect is earned NOT given.

  13. #13
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Count number of contiguous values in a range which meet conditions

    Thanks Sktneer..
    I have removed that...

    Thanks for appreciation...

  14. #14
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: Count number of contiguous values in a range which meet conditions

    Sorry, duplicate post.
    Last edited by kvsrinivasamurthy; 09-15-2014 at 02:28 AM.

  15. #15
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,587

    Re: Count number of contiguous values in a range which meet conditions

    Pl see attached file.
    One more trail of UDF
    Code
    Please Login or Register  to view this content.
    Attached Files Attached Files

  16. #16
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Count number of contiguous values in a range which meet conditions

    Thanks for reputation point..

    You should also tell which solution you have chosen..

  17. #17
    Registered User
    Join Date
    05-30-2014
    Posts
    11

    Re: Count number of contiguous values in a range which meet conditions

    Vikas,

    Thankyou. Your hard work is very much appreciated. The UDF works very well and it will enable me to perform this function much more easily and more accurately than doing the counting manually!

    kvsrinivasamurthy,

    Your solution also works!

    Thanks you both very much

  18. #18
    Forum Expert Vikas_Gautam's Avatar
    Join Date
    06-04-2013
    Location
    Ludhiana,Punjab, India
    MS-Off Ver
    Excel 2013
    Posts
    1,850

    Re: Count number of contiguous values in a range which meet conditions

    Thank you Tony, ChemistB, LaughingCrow & KVS..
    Actually It has been possible because of you all..

    Thanks for reputation points as well..
    I just can't express my happiness in words...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Count unique values within a range that meet a specific condition
    By CSS in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 06-24-2013, 02:00 PM
  2. Count number of rows that meet two conditions
    By travisg in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 06-18-2013, 12:20 PM
  3. How to only count the number of visible rows that meet 2 conditions
    By chaz123 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-04-2013, 10:15 AM
  4. Count # of rows that meet one of two conditions?
    By MrZ113 in forum Excel General
    Replies: 3
    Last Post: 02-21-2011, 11:23 AM
  5. Count matches that meet 2 conditions.
    By frankd in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 01-04-2010, 04:49 PM

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