Results 1 to 5 of 5

Thread: *Resolved* Determining Longest Run of Consecutive Numbers

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    *Resolved* Determining Longest Run of Consecutive Numbers

    Hi all,

    How would I get Excel to determine the longest 'run' of a given number in a list of numbers? E.g. suppose I have the following data in column A:

    24
    34
    0
    12
    0
    0
    67
    54
    0
    0
    0
    99

    Now suppose I want Excel to look at that data and tell me what the longest consecutive run of zero's are. In this case it would be 3 zero's. But how does I do this?

    Thanks
    -Rob
    Last edited by TheRobster; Apr 5th, 2005 at 10:44 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MS Excel Question

    No easy to do this other then a loop and a counter.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Re: MS Excel Question

    Hi RobDog,

    Can you elaborate please? I don't know what you mean.

    *Edit* The number I want to look at will always be zero, if this makes it any easier to do.

    Thanks
    -Rob

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: MS Excel Question

    Something like iterating through the cells like so.
    VB Code:
    1. dim i as integer
    2. dim iPrev as integer
    3.  
    4. For i = 1 to ActiveSheet.Cells.Range.SpecialCells(xlTypeCellLastCell).Row
    5.  
    6.     If Cells(i, 1).Value = 0 then
    7.         'increment a continuity counter.
    8.         if iPrev = i - 1 then
    9.             'add to an array or some list or continuous rows.
    10.         endif
    11.     endif
    12. next
    I just typed it in the reply box so it probably has errors but it should get you started.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Re: MS Excel Question

    Obtained a solution from another forum. Just in case anyone needs a similar one in future, here it is:

    I'm not sure if this will do, but try the following...

    1) Enter the text value 'End' at the end of your list of numbers, in this case A13.

    2) Enter the following formula in B1, and copy down:

    =IF(A1=0,MATCH(TRUE,A1:A13<>0,0)-1,"")

    ...confirmed with CONTROL+SHIFT+ENTER, not just ENTER.

    3) Enter the following formula in C1:

    =MAX(B1:B12)

    Notice that the range used by the MATCH function includes A13, which contains the text value 'End'. You could always choose to hide this value using 'Conditional Formatting', if you wish.

    Hope this helps!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width