+ Reply to Thread
Results 1 to 15 of 15

Maximum Number of Overlapping Intervals

Hybrid View

  1. #1
    Registered User
    Join Date
    01-28-2021
    Location
    Kedah
    MS-Off Ver
    2016
    Posts
    40

    Maximum Number of Overlapping Intervals

    Hi,

    Sample data : "Updated Sample, post #5"
    -Real data 100k+ row
    -Decimal points number

    Step:

    1) Start count maximum number of overlapping intervals for "Row 2 Interval (C$2,D$2)" ,"=IF(MAX(D$2:D3)<=MIN(C$2:C3),1,"")" at row 3 to find out if "Row 3 Interval (C3,D3)" overlaps "Row 2 Interval(C$2,D$2)

    2) If yes, continue to "Row 4 Interval (C4,D4)" so the formula become "=IF(MAX(D$2:D4)<=MIN(C$2:C4),1,"")"

    3) Stop the formula when it return "" and count all "1". Sum of "1" place at Row 2, Column F.

    4) Loop to next interval, start count maximum number of overlapping intervals for "Row 3 Interval (C$3,D$3)" , "=IF(MAX(D$3:D4)<=MIN(C$3:C4),1,"")"

    5) If yes, continue to "Row 5 Interval (C5,D5)" so the formula become "=IF(MAX(D$3:D5)<=MIN(C$3:C5),1,"")"

    6) Stop the formula when it return "" and count all "1". Sum of "1" place at Row 3, Column F.

    .
    .
    .
    Last edited by your; 06-05-2024 at 09:34 AM.

  2. #2
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,655

    Re: Maximum Number of Overlapping Intervals

    Not sure I have read yours 100%, but try with below code;

    PHP Code: 
    Option Explicit
    Sub overlap
    ()
    Dim lr&, i&, k&, c&
    Dim ABmin As Doublemax As Doubleres(), res2()
    lr Cells(Rows.Count"C").End(xlUp).Row ' last used row
    A = Range("C2:C" & lr).Value ' 
    point A value
    Range("D2:D" lr).Value ' point B value
    ReDim res(1 To UBound(A), 1 To 1): ReDim res2(1 To UBound(A), 1 To 1)
    min = 1000000: c = 0
    For i = 1 To UBound(A)
        If A(i, 1) < min Then min = A(i, 1)
        If B(i, 1) > max Then max = B(i, 1)
        If max <= min Then
            c = c + 1
            res(i, 1) = c ' 
    res is counting increasingly (0 to max)
        Else
            
    0min 1000000max 0
            res
    (i1) = ' res is re-starting from 0
            i = i + c
        End If
    Next
    max = res(UBound(res), 1)
    For i = UBound(A) To 1 Step -1
        res2(i, 1) = max - res(i, 1) ' 
    res2 is counting descreasingly
        
    If res(i1) = 0 Then max res(11)
    Next
    Range
    ("F2:F1000000").ClearContents
    Range
    ("F2").Resize(UBound(res), 1).Value res2 ' paste res2 to sheet
    End Sub 
    Attached Files Attached Files
    Quang PT

  3. #3
    Registered User
    Join Date
    01-28-2021
    Location
    Kedah
    MS-Off Ver
    2016
    Posts
    40

    Re: Maximum Number of Overlapping Intervals

    bebo021999,

    1. At F16 should be 0 because interval at row 17(15,13) do not overlap interval at row 16(11,9)

    2. The data, not necessary count decreasing after starting interval, example here (row 3) : "Updated Sample, post #5"

    It might same count (number of overlap) , F6, F7 : "Updated Sample, post #5"
    Last edited by your; 06-05-2024 at 09:16 AM.

  4. #4
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Maximum Number of Overlapping Intervals

    I don't fully understand the requirements, but try this:
    Sub your_2()
    Dim i As Long, n As Long, mm As Long
    Dim va, vb
    Dim xMin As Double, xMax As Double
    mm = 100000000
    va = Range("C2", Cells(Rows.Count, "C").End(xlUp)).Resize(, 2)
    ReDim vb(1 To UBound(va, 1), 1 To 1)
    xMin = mm
    For i = 2 To UBound(va, 1) - 1
        If xMin > va(i, 1) Then xMin = va(i, 1)
        If xMax < va(i, 2) Then xMax = va(i, 2)
    
        If xMin >= va(i + 1, 2) And xMax <= va(i + 1, 1) Then
            'do nothing
        Else
            vb(i, 1) = 0
            xMin = mm:  xMax = 0
        End If
    Next
    
    For i = UBound(vb, 1) To 2 Step -1
        n = 0
            Do While vb(i - 1, 1) = ""
                vb(i - 1, 1) = n + 1
                n = n + 1:  i = i - 1
                If i = 1 Then Exit For
            Loop
    Next
    'put the result in col E
    Range("E2:E500000").ClearContents
    Range("E2").Resize(UBound(vb, 1), 1) = vb
    End Sub

  5. #5
    Registered User
    Join Date
    01-28-2021
    Location
    Kedah
    MS-Off Ver
    2016
    Posts
    40

    Re: Maximum Number of Overlapping Intervals

    Akuini,

    Not working on some cases.
    This can be a little difficult to put into words, let me try explaining another way.
    I have updated another sample with explanation in sheet :

    Example 2 3.xlsb

  6. #6
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Maximum Number of Overlapping Intervals

    Sorry, but it's too difficult for me. I hope someone else comes up with a workable solution.

  7. #7
    Registered User
    Join Date
    01-28-2021
    Location
    Kedah
    MS-Off Ver
    2016
    Posts
    40

    Re: Maximum Number of Overlapping Intervals

    Does the expected result achievable in Excel or do I need use other tool?

    My last resort would be hire Excel freelancer or other tool freelancer.
    Just let me know if the expected result is achievable in Excel or other tool (please suggest).

    Thanks.

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,989

    Re: Maximum Number of Overlapping Intervals

    I think it's possible, but with Excel 2016 it's probably trickier than it would be with 365. Hang on in here!
    Last edited by AliGW; 06-10-2024 at 04:06 AM. Reason: Typo fixed.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,989

    Re: Maximum Number of Overlapping Intervals

    In I12, you say "Stop row 5" - please explain why.

  10. #10
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Maximum Number of Overlapping Intervals

    I gave it another shot, see if this works:
    The result is in col E
    Sub your_5_try()
    Dim i As Long, n As Long, q As Long, j As Long
    Dim va, vb
    n = Range("C" & Rows.Count).End(xlUp).Row
    va = Range("C2:C" & n)
    vb = Range("D2:D" & n)
    ReDim vc(1 To UBound(va, 1), 1 To 1)
    
    'IF(MAX(D$1:D2)<=MIN(C$1:C2);1;"")
    For i = 1 To UBound(va, 1) - 1
        q = i
        vc(i, 1) = 0
        Do
            q = q + 1
            If q > UBound(va, 1) Then Exit Do
            If to_Max(vb, i, q) <= to_Min(va, i, q) Then
                vc(i, 1) = vc(i, 1) + 1
            Else
                Exit Do
            End If
        Loop
    Next
    
    'put the result:
    Range("E2").Resize(UBound(vc, 1), 1) = vc
    
    End Sub
    
    Function to_Max(vx As Variant, a As Long, b As Long) As Double
    Dim h As Double, i As Long
    h = vx(a, 1)
    For i = a + 1 To b
        If h < vx(i, 1) Then h = vx(i, 1)
    Next
    to_Max = h
    End Function
    
    Function to_Min(vx As Variant, a As Long, b As Long) As Double
    Dim h As Double, i As Long
    h = vx(a, 1)
    For i = a + 1 To b
        If h > vx(i, 1) Then h = vx(i, 1)
    Next
    to_Min = h
    End Function

  11. #11
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Maximum Number of Overlapping Intervals

    @your
    Have you tried the code in post #10?

  12. #12
    Registered User
    Join Date
    01-28-2021
    Location
    Kedah
    MS-Off Ver
    2016
    Posts
    40

    Re: Maximum Number of Overlapping Intervals

    @AliGW
    Because at row 5, =IF(MAX(D$2:D5)<=MIN(C$2:C5),1,"") return "" . Once the formula return "", no need to continue the formula in the next row.


    @Akuini
    Sorry for late reply, I took a time to test the code on several data to make sure it is working for all type of data,
    and the result is ACHIEVABLE by your code. Thank you very much.

    I have one last request but if it too much work or need alter many lines of orginal code(start a new code), then it is fine.
    I just can get the result by flip the data (sort "List Order" from Largest to Smallest") and run the code.
    My last request is start the count from last row to top.
    But if it too much for you , then it is ok, for now I just can get the result by flip the data and run the code.

    Last Row to Top : Example 2 3 - Reverse.xlsb
    Last edited by your; 06-11-2024 at 11:24 AM.

  13. #13
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Maximum Number of Overlapping Intervals

    My last request is start the count from last row to top.
    Try this:
    Sub your_5_reverse()
    Dim i As Long, n As Long, q As Long, j As Long
    Dim va, vb
    n = Range("C" & Rows.Count).End(xlUp).Row
    va = Range("C2:C" & n)
    vb = Range("D2:D" & n)
    ReDim vc(1 To UBound(va, 1), 1 To 1)
    
    'IF(MAX(D$1:D2)<=MIN(C$1:C2);1;"")
    For i = UBound(va, 1) To 2 Step -1
        q = i
        vc(i, 1) = 0
        Do
            q = q - 1
            If q = 0 Then Exit Do
            If to_Max(vb, q, i) <= to_Min(va, q, i) Then
                vc(i, 1) = vc(i, 1) + 1
            Else
                Exit Do
            End If
        Loop
    Next
    
    'put the result:
    Range("E2").Resize(UBound(vc, 1), 1) = vc
    
    End Sub

  14. #14
    Registered User
    Join Date
    01-28-2021
    Location
    Kedah
    MS-Off Ver
    2016
    Posts
    40

    Re: Maximum Number of Overlapping Intervals

    @Akuini, thanks again for the second code, I really appreciate your effort help me.

  15. #15
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,028

    Re: Maximum Number of Overlapping Intervals

    You're welcome, glad to help & thanks for the feedback.
    And thanks for the Rep+

+ 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. [SOLVED] Need for formula to calculate overlapping time intervals of specific categories
    By rid1whitehead in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-12-2022, 11:27 AM
  2. Combining Overlapping Dates between Unique ID & Identify Overlapping with Other Date Range
    By bihaequipinc in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 3
    Last Post: 09-28-2021, 02:11 AM
  3. [SOLVED] Overlapping Time Intervals - Formula to Show Start Overlaps
    By ARec3 in forum Excel General
    Replies: 3
    Last Post: 10-16-2017, 09:13 AM
  4. Replies: 9
    Last Post: 07-20-2017, 01:35 PM
  5. Calculating the overlapping time intervals
    By Mukund03 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-26-2015, 07:20 PM
  6. Overlapping Time Intervals of the Same Date
    By cworkman123 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-12-2015, 11:21 AM
  7. Sum overlapping start end time intervals
    By Jack-of-Trades in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 10-15-2014, 12:56 PM

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