+ Reply to Thread
Results 1 to 37 of 37

Need help find longest group of numbers

Hybrid View

  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...
    Sub tgr()
        
        Dim rngCell As Range
        Dim lCount As Long
        Dim lMax As Long
        
        For Each rngCell In Intersect(ActiveSheet.UsedRange, Columns("C")).Cells
            If rngCell.Value < 0 Then lCount = lCount + 1 Else lCount = 0
            lMax = WorksheetFunction.Max(lMax, lCount)
        Next rngCell
        
        MsgBox lMax
        
    End Sub
    Hope that helps,
    ~tigeravatar

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

  3. #3
    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

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

    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

  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

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

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

    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.

  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 longest occurred on the list?

  8. #8
    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

  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...
    Sub tgr()
        
        Dim rngCheck As Range
        Dim rngCell As Range
        Dim arrGroups(1 To 65000) As Long
        Dim rngGroups(1 To 65000) As Range
        Dim lCount As Long
        Dim lMax As Long
        Dim lCommon As Long
        Dim i As Long
        
        Set rngCheck = Intersect(ActiveSheet.UsedRange, Columns("C"))
        Set rngCheck = rngCheck.Resize(rngCheck.Rows.Count + 1)
        
        For Each rngCell In rngCheck.Cells
            If rngCell.Value < 0 Then
                lCount = lCount + 1
            Else
                If lCount > 0 Then
                    If lCount > lMax Then lMax = lCount
                    arrGroups(lCount) = arrGroups(lCount) + 1
                    Select Case (rngGroups(lCount) Is Nothing)
                        Case True:  Set rngGroups(lCount) = rngCell.Offset(-lCount).Resize(lCount)
                        Case Else:  Set rngGroups(lCount) = Union(rngGroups(lCount), rngCell.Offset(-lCount).Resize(lCount))
                    End Select
                    lCount = 0
                End If
            End If
        Next rngCell
        
        If lMax > 0 Then
            For i = 1 To lMax
                If arrGroups(i) > lCommon Then lCommon = i
            Next i
            MsgBox "Most common group size is [" & lCommon & "] appearing " & arrGroups(lCommon) & " time(s) and in location(s):" & Chr(10) & _
                   rngGroups(lCommon).Address(0, 0) & Chr(10) & _
                   Chr(10) & _
                   "Maximum group size is [" & lMax & "] appearing " & arrGroups(lMax) & " time(s) and in location(s):" & Chr(10) & _
                   rngGroups(lMax).Address(0, 0)
        End If
        
        Set rngCheck = Nothing
        Set rngCell = Nothing
        Erase arrGroups
        
    End Sub

  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

    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.

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

    VBisgreat,

    Updated code:
    Sub tgr()
        
        Dim rngCheck As Range
        Dim rngCell As Range
        Dim rngArea As Range
        Dim arrGroups(1 To 65000) As Long
        Dim rngGroups(1 To 65000) As Range
        Dim dAvg(1 To 2) As Double
        Dim lCount As Long
        Dim lMax As Long
        Dim lCommon As Long
        Dim i As Long
        
        Set rngCheck = Intersect(ActiveSheet.UsedRange, Columns("C"))
        Set rngCheck = rngCheck.Resize(rngCheck.Rows.Count + 1)
        
        For Each rngCell In rngCheck.Cells
            If rngCell.Value < 0 Then
                lCount = lCount + 1
            Else
                If lCount > 0 Then
                    If lCount > lMax Then lMax = lCount
                    arrGroups(lCount) = arrGroups(lCount) + 1
                    If arrGroups(lCount) > lCommon Then lCommon = lCount
                    Select Case (rngGroups(lCount) Is Nothing)
                        Case True:  Set rngGroups(lCount) = rngCell.Offset(-lCount).Resize(lCount)
                        Case Else:  Set rngGroups(lCount) = Union(rngGroups(lCount), rngCell.Offset(-lCount).Resize(lCount))
                    End Select
                    lCount = 0
                End If
            End If
        Next rngCell
        
        If lMax > 0 Then
            For Each rngArea In rngGroups(lCommon).Areas
                dAvg(1) = dAvg(1) + rngArea.Offset(-1).Cells(1).Value
            Next rngArea
            dAvg(1) = dAvg(1) / rngGroups(lCommon).Areas.Count
            For Each rngArea In rngGroups(lMax).Areas
                dAvg(2) = dAvg(2) + rngArea.Offset(-1).Cells(1).Value
            Next rngArea
            dAvg(2) = dAvg(2) / rngGroups(lMax).Areas.Count
            MsgBox "Most common group size: " & lCommon & Chr(10) & _
                   "Appearances: " & arrGroups(lCommon) & Chr(10) & _
                   "Locations: " & rngGroups(lCommon).Address(0, 0) & Chr(10) & _
                   "Positive Average: " & dAvg(1) & Chr(10) & _
                   Chr(10) & _
                   "Maximum group size:" & lMax & Chr(10) & _
                   "Appearances: " & arrGroups(lMax) & Chr(10) & _
                   "Location(s): " & rngGroups(lMax).Address(0, 0) & Chr(10) & _
                   "Positive Average: " & dAvg(2)
        End If
        
        Set rngCheck = Nothing
        Set rngCell = Nothing
        Set rngArea = Nothing
        Erase arrGroups
        Erase dAvg
        
    End Sub

  12. #12
    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.

  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.
    Sub tgr()
        
        Dim rngCheck As Range
        Dim rngCell As Range
        Dim rngArea As Range
        Dim arrGroups(1 To 65000) As Long
        Dim rngGroups(1 To 65000) As Range
        Dim dAvg(1 To 2) As Double
        Dim lCount As Long
        Dim lMax As Long
        Dim lCommon As Long
        Dim CommonIndex As Long
        Dim i As Long
        
        Set rngCheck = Intersect(ActiveSheet.UsedRange, Columns("C"))
        Set rngCheck = rngCheck.Resize(rngCheck.Rows.Count + 1)
        
        For Each rngCell In rngCheck.Cells
            If rngCell.Value < 0 Then
                lCount = lCount + 1
            Else
                If lCount > 0 Then
                    If lCount > lMax Then lMax = lCount
                    arrGroups(lCount) = arrGroups(lCount) + 1
                    'MsgBox "Streak: " & lCount & Chr(10) & "Quantity of streak: " & arrGroups(lCount)
                    If arrGroups(lCount) > lCommon Then
                        lCommon = arrGroups(lCount)
                        CommonIndex = lCount
                    End If
                    Select Case (rngGroups(lCount) Is Nothing)
                        Case True:  Set rngGroups(lCount) = rngCell.Offset(-lCount).Resize(lCount)
                        Case Else:  Set rngGroups(lCount) = Union(rngGroups(lCount), rngCell.Offset(-lCount).Resize(lCount))
                    End Select
                    lCount = 0
                End If
            End If
        Next rngCell
        
        If lMax > 0 Then
            For Each rngArea In rngGroups(CommonIndex).Areas
                dAvg(1) = dAvg(1) + rngArea.Offset(-1).Cells(1).Value
            Next rngArea
            dAvg(1) = dAvg(1) / rngGroups(CommonIndex).Areas.Count
            For Each rngArea In rngGroups(lMax).Areas
                dAvg(2) = dAvg(2) + rngArea.Offset(-1).Cells(1).Value
            Next rngArea
            dAvg(2) = dAvg(2) / rngGroups(lMax).Areas.Count
            MsgBox "Most common group size: " & CommonIndex & Chr(10) & _
                   "Appearances: " & arrGroups(CommonIndex) & Chr(10) & _
                   "Locations: " & rngGroups(CommonIndex).Address(0, 0) & Chr(10) & _
                   "Positive Average: " & dAvg(1) & Chr(10) & _
                   Chr(10) & _
                   "Maximum group size: " & lMax & Chr(10) & _
                   "Appearances: " & arrGroups(lMax) & Chr(10) & _
                   "Location(s): " & rngGroups(lMax).Address(0, 0) & Chr(10) & _
                   "Positive Average: " & dAvg(2)
        End If
        
        Set rngCheck = Nothing
        Set rngCell = Nothing
        Set rngArea = Nothing
        Erase arrGroups
        Erase dAvg
        
    End Sub

  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:
    Sub tgr()
        
        Dim rngCheck As Range
        Dim rngCell As Range
        Dim rngArea As Range
        Dim arrGroups(1 To 65000) As Long
        Dim rngGroups(1 To 65000) As Range
        Dim dAvg(1 To 2) As Double
        Dim lCount As Long
        Dim lMax As Long
        Dim lCommon As Long
        Dim CommonIndex As Long
        Dim i As Long
        
        Set rngCheck = Intersect(ActiveSheet.UsedRange, Columns("C"))
        Set rngCheck = rngCheck.Resize(rngCheck.Rows.Count + 1)
        
        For Each rngCell In rngCheck.Cells
            If rngCell.Value < 0 Then
                lCount = lCount + 1
            Else
                If lCount > 1 Then
                    If lCount > lMax Then lMax = lCount
                    arrGroups(lCount) = arrGroups(lCount) + 1
                    'MsgBox "Streak: " & lCount & Chr(10) & "Quantity of streak: " & arrGroups(lCount)
                    If arrGroups(lCount) > lCommon Then
                        lCommon = arrGroups(lCount)
                        CommonIndex = lCount
                    End If
                    Select Case (rngGroups(lCount) Is Nothing)
                        Case True:  Set rngGroups(lCount) = rngCell.Offset(-lCount).Resize(lCount)
                        Case Else:  Set rngGroups(lCount) = Union(rngGroups(lCount), rngCell.Offset(-lCount).Resize(lCount))
                    End Select
                End If
                lCount = 0
            End If
        Next rngCell
        
        If lMax > 0 Then
            For Each rngArea In rngGroups(CommonIndex).Areas
                dAvg(1) = dAvg(1) + rngArea.Offset(-1).Cells(1).Value
            Next rngArea
            dAvg(1) = dAvg(1) / rngGroups(CommonIndex).Areas.Count
            For Each rngArea In rngGroups(lMax).Areas
                dAvg(2) = dAvg(2) + rngArea.Offset(-1).Cells(1).Value
            Next rngArea
            dAvg(2) = dAvg(2) / rngGroups(lMax).Areas.Count
            MsgBox "Most common group size: " & CommonIndex & Chr(10) & _
                   "Appearances: " & arrGroups(CommonIndex) & Chr(10) & _
                   "Locations: " & rngGroups(CommonIndex).Address(0, 0) & Chr(10) & _
                   "Positive Average: " & dAvg(1) & Chr(10) & _
                   Chr(10) & _
                   "Maximum group size: " & lMax & Chr(10) & _
                   "Appearances: " & arrGroups(lMax) & Chr(10) & _
                   "Location(s): " & rngGroups(lMax).Address(0, 0) & Chr(10) & _
                   "Positive Average: " & dAvg(2)
        End If
        
        Set rngCheck = Nothing
        Set rngCell = Nothing
        Set rngArea = Nothing
        Erase arrGroups
        Erase dAvg
        
    End Sub

  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.
    Sub tgr()
        
        Dim rngCheck As Range
        Dim rngCell As Range
        Dim rngArea As Range
        Dim rngGroups(2 To 65000) As Range
        Dim arrGroups(2 To 65000) As Long
        Dim dAvg(2 To 65000) As Double
        Dim arrLoc(2 To 65000) As String
        Dim lCount As Long
        Dim lMax As Long
        Dim lCommon As Long
        Dim CommonIndex As Long
        Dim i As Long
        
        Set rngCheck = Intersect(ActiveSheet.UsedRange, Columns("C"))
        Set rngCheck = rngCheck.Resize(rngCheck.Rows.Count + 1)
        
        For Each rngCell In rngCheck.Cells
            If rngCell.Value < 0 Then
                lCount = lCount + 1
            Else
                If lCount > 1 Then
                    If lCount > lMax Then lMax = lCount
                    arrGroups(lCount) = arrGroups(lCount) + 1
                    'MsgBox "Streak: " & lCount & Chr(10) & "Quantity of streak: " & arrGroups(lCount)
                    If arrGroups(lCount) > lCommon Then
                        lCommon = arrGroups(lCount)
                        CommonIndex = lCount
                    End If
                    Select Case (rngGroups(lCount) Is Nothing)
                        Case True:  Set rngGroups(lCount) = rngCell.Offset(-lCount).Resize(lCount)
                        Case Else:  Set rngGroups(lCount) = Union(rngGroups(lCount), rngCell.Offset(-lCount).Resize(lCount))
                    End Select
                    arrLoc(lCount) = arrLoc(lCount) & "," & rngCell.Offset(-lCount).Resize(lCount).Address(0, 0)
                End If
                lCount = 0
            End If
        Next rngCell
        
        If lMax > 0 Then
            For i = 2 To lMax
                If Not rngGroups(i) Is Nothing Then
                    For Each rngArea In rngGroups(i).Areas
                        dAvg(i) = dAvg(i) + rngArea.Offset(-1).Cells(1).Value
                    Next rngArea
                    dAvg(i) = dAvg(i) / rngGroups(i).Areas.Count
                    arrLoc(i) = Mid(arrLoc(i), 2)
                Else
                    dAvg(i) = 0
                End If
            Next i
            
            With Sheets.Add
                .Range("A2").Resize(lMax - 1).Value = Application.Transpose(Application.Transpose(Evaluate("Index(Row(2:" & lMax & "),)")))
                .Range("B2").Resize(lMax - 1).Value = Application.Transpose(arrGroups)
                .Range("C2").Resize(lMax - 1).Value = Application.Transpose(dAvg)
                .Range("D2").Resize(lMax - 1).Value = Application.Transpose(arrLoc)
                With .Range("A1:D1")
                    .Value = Array("Group Size", "Appearances", "Positive Average", "Location")
                    .Font.Bold = True
                    .Borders(xlEdgeBottom).LineStyle = xlContinuous
                    .Resize(, 3).EntireColumn.AutoFit
                End With
                .UsedRange.Sort .Range("B1"), xlDescending, Header:=xlYes
                .Columns("A").Find(lMax).Resize(, 4).Interior.ColorIndex = 6
            End With
        End If
        
        Set rngCheck = Nothing
        Set rngCell = Nothing
        Set rngArea = Nothing
        Erase rngGroups
        Erase arrGroups
        Erase dAvg
        Erase arrLoc
        
    End Sub

  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

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

  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

    Ah, I was able to replicate the error. Update that line of code to the following:
    dAvg(i) = dAvg(i) + Val(rngArea.Offset(-1).Cells(1).Value)

  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

    Updated code:
    Sub tgr()
        
        Dim rngCheck As Range
        Dim rngCell As Range
        Dim rngArea As Range
        Dim rngGroups(1 To 65000) As Range
        Dim arrGroups(1 To 65000) As Long
        Dim dAvg(1 To 65000) As Double
        Dim arrLoc(1 To 65000) As String
        Dim lCount As Long
        Dim lMax As Long
        Dim lCommon As Long
        Dim CommonIndex As Long
        Dim i As Long
        
        Set rngCheck = Intersect(ActiveSheet.UsedRange, Columns("C"))
        Set rngCheck = rngCheck.Resize(rngCheck.Rows.Count + 1)
        
        For Each rngCell In rngCheck.Cells
            If rngCell.Value < 0 Then
                lCount = lCount + 1
            Else
                If lCount > 0 Then
                    If lCount > lMax Then lMax = lCount
                    arrGroups(lCount) = arrGroups(lCount) + 1
                    'MsgBox "Streak: " & lCount & Chr(10) & "Quantity of streak: " & arrGroups(lCount)
                    If arrGroups(lCount) > lCommon Then
                        lCommon = arrGroups(lCount)
                        CommonIndex = lCount
                    End If
                    Select Case (rngGroups(lCount) Is Nothing)
                        Case True:  Set rngGroups(lCount) = rngCell.Offset(-lCount).Resize(lCount)
                        Case Else:  Set rngGroups(lCount) = Union(rngGroups(lCount), rngCell.Offset(-lCount).Resize(lCount))
                    End Select
                    arrLoc(lCount) = arrLoc(lCount) & "," & rngCell.Offset(-lCount).Resize(lCount).Address(0, 0)
                End If
                lCount = 0
            End If
        Next rngCell
        
        If lMax > 0 Then
            For i = 1 To lMax
                If Not rngGroups(i) Is Nothing Then
                    For Each rngArea In rngGroups(i).Areas
                        dAvg(i) = dAvg(i) + rngArea.Offset(-1).Cells(1).Value
                    Next rngArea
                    dAvg(i) = dAvg(i) / rngGroups(i).Areas.Count
                    arrLoc(i) = Mid(arrLoc(i), 2)
                Else
                    dAvg(i) = 0
                End If
            Next i
            
            With Sheets.Add
                .Range("A2").Resize(lMax - 1).Value = Application.Transpose(Application.Transpose(Evaluate("Index(Row(2:" & lMax & "),)")))
                .Range("B2").Resize(lMax - 1).Value = Application.Transpose(arrGroups)
                .Range("C2").Resize(lMax - 1).Value = Application.Transpose(dAvg)
                .Range("D2").Resize(lMax - 1).Value = Application.Transpose(arrLoc)
                With .Range("A1:D1")
                    .Value = Array("Group Size", "Appearances", "Positive Average", "Location")
                    .Font.Bold = True
                    .Borders(xlEdgeBottom).LineStyle = xlContinuous
                    .Resize(, 3).EntireColumn.AutoFit
                End With
                .UsedRange.Sort .Range("B1"), xlDescending, Header:=xlYes
                .Columns("A").Find(lMax).Resize(, 4).Interior.ColorIndex = 6
            End With
        End If
        
        Set rngCheck = Nothing
        Set rngCell = Nothing
        Set rngArea = Nothing
        Erase rngGroups
        Erase arrGroups
        Erase dAvg
        Erase arrLoc
        
    End Sub

  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

    Just change this line:
    If lCount > 1 Then

    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

  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
    Sub tgr()
        
        Dim rngCheck As Range
        Dim rngCell As Range
        Dim rngArea As Range
        Dim rngGroups(1 To 65000) As Range
        Dim arrGroups(1 To 65000) As Long
        Dim dAvg(1 To 65000) As Double
        Dim arrLoc(1 To 65000) As String
        Dim lCount As Long
        Dim lMax As Long
        Dim lCommon As Long
        Dim CommonIndex As Long
        Dim i As Long
        
        Set rngCheck = Intersect(ActiveSheet.UsedRange, Columns("C"))
        Set rngCheck = rngCheck.Resize(rngCheck.Rows.Count + 1)
        
        For Each rngCell In rngCheck.Cells
            If rngCell.Value < 0 Then
                lCount = lCount + 1
            Else
                If lCount > 0 Then
                    If lCount > lMax Then lMax = lCount
                    arrGroups(lCount) = arrGroups(lCount) + 1
                    If arrGroups(lCount) > lCommon Then
                        lCommon = arrGroups(lCount)
                        CommonIndex = lCount
                    End If
                    Select Case (rngGroups(lCount) Is Nothing)
                        Case True:  Set rngGroups(lCount) = rngCell.Offset(-lCount).Resize(lCount)
                        Case Else:  Set rngGroups(lCount) = Union(rngGroups(lCount), rngCell.Offset(-lCount).Resize(lCount))
                    End Select
                    arrLoc(lCount) = arrLoc(lCount) & "," & rngCell.Offset(-lCount).Resize(lCount).Address(0, 0)
                End If
                lCount = 0
            End If
        Next rngCell
        
        If lMax > 0 Then
            For i = 1 To lMax
                If Not rngGroups(i) Is Nothing Then
                    For Each rngArea In rngGroups(i).Areas
                        dAvg(i) = dAvg(i) + rngArea.Offset(-1).Cells(1).Value
                    Next rngArea
                    dAvg(i) = dAvg(i) / rngGroups(i).Areas.Count
                    arrLoc(i) = Mid(arrLoc(i), 2)
                Else
                    dAvg(i) = 0
                End If
            Next i
            
            With Sheets.Add
                .Range("A2").Resize(lMax).Value = Application.Transpose(Application.Transpose(Evaluate("Index(Row(1:" & lMax & "),)")))
                .Range("B2").Resize(lMax).Value = Application.Transpose(arrGroups)
                .Range("C2").Resize(lMax).Value = Application.Transpose(dAvg)
                .Range("D2").Resize(lMax).Value = Application.Transpose(arrLoc)
                With .Range("A1:D1")
                    .Value = Array("Group Size", "Appearances", "Positive Average", "Location")
                    .Font.Bold = True
                    .Borders(xlEdgeBottom).LineStyle = xlContinuous
                    .Resize(, 3).EntireColumn.AutoFit
                End With
                .UsedRange.Sort .Range("B1"), xlDescending, Header:=xlYes
                .Columns("A").Find(lMax).Resize(, 4).Interior.ColorIndex = 6
            End With
        End If
        
        Set rngCheck = Nothing
        Set rngCell = Nothing
        Set rngArea = Nothing
        Erase rngGroups
        Erase arrGroups
        Erase dAvg
        Erase arrLoc
        
    End Sub

  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:
    Sub tgr()
        
        Dim rngCheck As Range
        Dim rngCell As Range
        Dim rngArea As Range
        Dim rngGroups(1 To 65000) As Range
        Dim arrGroups(1 To 65000) As Long
        Dim dAvg(1 To 65000) As Double
        Dim arrLoc(1 To 65000) As String
        Dim lCount As Long
        Dim lMax As Long
        Dim lCommon As Long
        Dim CommonIndex As Long
        Dim i As Long
        
        Set rngCheck = Intersect(ActiveSheet.UsedRange, Columns("C"))
        Set rngCheck = rngCheck.Resize(rngCheck.Rows.Count + 1)
        
        For Each rngCell In rngCheck.Cells
            If rngCell.Value < 0 Then
                lCount = lCount + 1
            Else
                If lCount > 0 Then
                    If lCount > lMax Then lMax = lCount
                    arrGroups(lCount) = arrGroups(lCount) + 1
                    If arrGroups(lCount) > lCommon Then
                        lCommon = arrGroups(lCount)
                        CommonIndex = lCount
                    End If
                    Select Case (rngGroups(lCount) Is Nothing)
                        Case True:  Set rngGroups(lCount) = rngCell.Offset(-lCount).Resize(lCount)
                        Case Else:  Set rngGroups(lCount) = Union(rngGroups(lCount), rngCell.Offset(-lCount).Resize(lCount))
                    End Select
                    arrLoc(lCount) = arrLoc(lCount) & "," & rngCell.Offset(-lCount).Resize(lCount).Address(0, 0)
                End If
                lCount = 0
            End If
        Next rngCell
        
        If lMax > 0 Then
            For i = 1 To lMax
                If Not rngGroups(i) Is Nothing Then
                    For Each rngArea In rngGroups(i).Areas
                        dAvg(i) = dAvg(i) + rngArea.Offset(-1).Cells(1).Value
                    Next rngArea
                    dAvg(i) = dAvg(i) / rngGroups(i).Areas.Count
                    arrLoc(i) = Mid(arrLoc(i), 2)
                Else
                    dAvg(i) = 0
                End If
            Next i
            
            Range("J26:M" & Rows.Count).ClearContents
            Range("J26").Resize(lMax).Value = Application.Transpose(Application.Transpose(Evaluate("Index(Row(1:" & lMax & "),)")))
            Range("K26").Resize(lMax).Value = Application.Transpose(arrGroups)
            Range("L26").Resize(lMax).Value = Application.Transpose(dAvg)
            Range("M26").Resize(lMax).Value = Application.Transpose(arrLoc)
            With Range("J25:M25")
                .Value = Array("Group Size", "Appearances", "Positive Average", "Location")
                .Font.Bold = True
                .Borders(xlEdgeBottom).LineStyle = xlContinuous
                '.Resize(, 3).EntireColumn.AutoFit
            End With
            Range("J25").Resize(lMax + 1, 4).Sort Range("K25"), xlDescending, Header:=xlYes
            Range("J25:J" & Rows.Count).Find(lMax).Resize(, 4).Interior.ColorIndex = 6
        End If
        
        Set rngCheck = Nothing
        Set rngCell = Nothing
        Set rngArea = Nothing
        Erase rngGroups
        Erase arrGroups
        Erase dAvg
        Erase arrLoc
        
    End Sub

+ 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