+ Reply to Thread
Results 1 to 37 of 37

Need help find longest group of numbers

  1. #1
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Need help find longest group of numbers

    I have a column of numbers in col C, the numbers vary from -2.00 to +2.00. I’d like a macro that will help find all the group of next-to-each-other negative numbers and compare them and let me know which one is the longest.
    EX:
    -0.25
    -0.72
    -0.43
    0.05
    0.62
    -0.05
    -0.29
    -0.06
    -0.34
    0.46
    1.52
    0.96
    -0.88
    0.55
    The answer is 4 because it has four numbers next to each other less than zero.

  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: Need help find longest group of numbers

    VBisgreat,

    One way...
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

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

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Need help find longest group of numbers

    How about just a formula?

    =MAX(FREQUENCY(IF(A1:A14<0, ROW(A1:A14)), IF(A1:A14>=0, ROW(A1:A14))))

    The formula MUST be confirmed with Ctrl+Shift+Enter
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    Is there a way to tell where longest occurred on the list?

  5. #5
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    Similarly, I want to know which group happen most often. In this case I want the answer to be 3 because the 3-next-to-each-other negative group appeared most often (twice). Thank you so much!!!

    Ex:
    -0.25
    -0.72
    -0.43
    0.05
    0.62
    -0.05
    -0.29
    -0.06
    0.46
    1.52
    0.96
    -0.88
    -452
    0.55
    1.00
    1.87
    -0.05
    -0.29
    -0.06
    -0.66
    1.34

  6. #6
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    The fomular doesn't work because the data is in col C. Thanks though!

  7. #7
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    Is there a way to tell where the on the list that group occurred? Can you help modify it so that this time instead of finding the longest it will find the one appear most frequent?
    ex:
    -0.25
    -0.72
    -0.43
    0.05
    0.62
    -0.05
    -0.29
    -0.06
    0.46
    1.52
    0.96
    -0.88
    0.55

    the answer is 3 because the three group appearred twice.

    Thank you,
    VB

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Need help find longest group of numbers

    Quote Originally Posted by VBisgreat View Post
    The fomular doesn't work because the data is in col C. Thanks though!
    That's quite an obstacle to overcome--good luck.

  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: Need help find longest group of numbers

    VBisgreat,

    Perhaps...
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    Thank you all.

  11. #11
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    Can you add this computation in there too?

    Ex:
    0.62
    -0.05
    -0.29
    -0.06
    0.46
    1.52
    0.96
    -0.34
    -0.88
    0.29
    -0.25
    -0.72
    -0.43
    0.05
    The most common group is 3 and the average of the positive numbers one place above them is (0.62+0.29)/2=0.455

    Do the same thing for maximum group. Find the average of the positive numbers one place above it.

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

    Re: Need help find longest group of numbers

    VBisgreat,

    Updated code:
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    thank you very much!

  14. #14
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    I tried running the data using the previous code and this code, the previous one came out for the common group 5 appeared 10 times, but this code shown common group as 4 appeared 8 time. The max groupd is good.
    Last edited by VBisgreat; 03-26-2013 at 02:52 PM.

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

    Re: Need help find longest group of numbers

    VBisgreat,

    I found the bug and here is the updated code. With that dataset, the most common streak is actually 1 with 88 occurrences. Would you rather that streaks of 1 be ignored? The second most common is streaks of 2 with 42 occurrences. This code will include streaks of 1.
    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    ignore 1 streak please. Thanks.

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

    Re: Need help find longest group of numbers

    This code will ignore streaks of 1:
    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    Can have them list from max group, most common, second common...ect? just wondering

    or just list them out max streak, second, third, ect... and I can look at it and determine the most common streak...
    Last edited by VBisgreat; 03-21-2013 at 04:28 PM.

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

    Re: Need help find longest group of numbers

    VBisgreat,

    This code will create a new worksheet and output the results to this new sheet. It will sort the results by number of appearances so that the most common is on top, 2nd-most common is next and so on. It will then highlight the maximum group size wherever it is in the list so that it can be easily referenced.
    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    Can you put it in a pop up window, I really like the pop-up window. Very convenience. Thanks.

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

    Re: Need help find longest group of numbers

    The information won't all fit in a msgbox (popup window).

  22. #22
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    error at: dAvg(i) = dAvg(i) + rngArea.Offset(-1).Cells(1).Value

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

    Re: Need help find longest group of numbers

    VBisgreat,

    I am able to run the code with no issues and it performs as expected. Attached is an example workbook that contains the macro. When I run it, I get the results sheet successfully.
    Attached Files Attached Files

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

    Re: Need help find longest group of numbers

    Ah, I was able to replicate the error. Update that line of code to the following:
    Please Login or Register  to view this content.

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

    Re: Need help find longest group of numbers

    Also, make sure you have a header cell so that actual data starts below row 1.

  26. #26
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    since you have it in another worksheet, can you include the 1 streak too? many thanks.

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

    Re: Need help find longest group of numbers

    Just change this line:
    Please Login or Register  to view this content.

    So that it is > 0 instead of > 1
    That will include the 1 streaks.

    oh wait, there are some other modifications too, just a sec

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

    Re: Need help find longest group of numbers

    Updated code:
    Please Login or Register  to view this content.

  29. #29
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    It's still ignoring the 1 streak.

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

    Re: Need help find longest group of numbers

    That's what I get for not testing before posting /sigh
    This one works
    Please Login or Register  to view this content.

  31. #31
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    I ran the two code and compared them but the answer from with and without 1 streak don't match up.
    size appear
    1 88
    2 42
    3 15
    4 9
    5 9
    6 4
    7 2
    8 1


    Size Appearances
    2 88
    3 42
    4 15
    5 9
    6 9
    7 4
    8 2

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

    Re: Need help find longest group of numbers

    The first one is correct, we already know this because I stated earlier that there are 88 1-streaks and 42 2-streaks
    The second size/appearances you listed are from the incorrect code that was "ignoring" 1-streaks. It wasn't ignoring them, but it wasn't outputting the display properly which is what I fixed in the most recent code.

  33. #33
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    Thank you very much!!!
    VBisgreat!!

  34. #34
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    keep getting error here sometimes

    dAvg(i) = dAvg(i) + rngArea.Offset(-1).Cells(1).Value

  35. #35
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    nevermind. Thanks!!

  36. #36
    Forum Contributor
    Join Date
    05-12-2009
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    219

    Re: Need help find longest group of numbers

    tigeravatar,

    Is there a way you can help me put the output (group size, appearances, ect..) on the same sheet instead of adding another sheet then put it there? it will help make it easier for me. Please put it starting on cell J25.

    Thanks sincerely,
    VBisgreat

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

    Re: Need help find longest group of numbers

    Updated code:
    Please Login or Register  to view this content.

+ 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