【问题标题】:Number of Overlapping datetime(s)重叠日期时间数
【发布时间】:2015-09-09 18:14:51
【问题描述】:

我有两列日期/时间,需要找出其中有多少在特定时间重叠。

用例是这样的:这些是​​电话呼叫的开始时间和结束时间,我正在寻找同时通话的数量。

Column A            Column B    
8/06/15 00:17:59    8/06/15 00:19:21     
8/09/15 00:21:06    8/09/15 00:22:06     
8/09/15 00:21:21    8/09/15 00:22:43     
8/09/15 00:22:11    8/09/15 00:22:46     
8/10/15 00:24:28    8/10/15 00:24:51     

预期结果:

Column A            Column B             Number Overlap    
8/06/15 00:17:59    8/06/15 00:19:21     0
8/09/15 00:21:06    8/09/15 00:22:06     1
8/09/15 00:21:21    8/09/15 00:22:43     2
8/09/15 00:22:11    8/09/15 00:22:46     1
8/10/15 00:24:28    8/10/15 00:24:51     0

我正在尝试的公式是这样的:

=SUMPRODUCT((A$2:A$35006<=B2)*(B$2:B$35006>=A2))

我还想参考类似的问题,这些问题并不能完全回答我在这里的需要,但帮助我做到了这一点:

Find number of concurrent, overlapping, date ranges

http://www.mrexcel.com/forum/excel-questions/388376-count-number-date-ranges-overlap-other-date-ranges.html

【问题讨论】:

  • 您是否需要寻找完全重叠的那些?所以他们在另一个之前开始,在那个之后结束。
  • 是的。任何重叠的东西。基本上,有多少是同时发生的。
  • 答案已更新以捕获完全重叠。

标签: vba excel date date-range


【解决方案1】:

我认为您只需要从当前公式中减去一个即可:

=SUMPRODUCT((A$2:A$35006<=B2)*(B$2:B$35006>=A2))-1

【讨论】:

  • 我似乎无法让它工作,所有结果都返回为 0。
  • 它在这里工作。它假定您的电话时间从第 2 行开始,然后从那里一直到第 35006 行。您必须确保所有条目实际上都不是 TEXT 而不是真实数字。
  • 如果您愿意,我可以提供帮助。把文件发给我,我会很快整理好。我的电子邮件地址是:daniel.ferry@gmail.com
  • 我现在意识到我提出这个问题的方式存在缺陷。它说“2”的地方应该只说“1”,因为一次只有一个重叠。是的,有两条记录重叠,但它们不交叉。
  • 请提出一个新问题。
【解决方案2】:

由于您的标签中有 VBA,因此您可以这样做。

在您的 VBA IDE 中,转到工具菜单并选择引用。选择“Microstoft ActiveX 数据对象 2.8 库。

Private Sub CheckOverlaps()
    Dim ws1 As Excel.Worksheet
    Dim iCount As Integer
    Dim rs As New ADODB.Recordset
    Dim lRow As Long

    'Set the worksheet that you want to process by name
    Set ws1 = ActiveWorkbook.Sheets("Sheet1")

    'Add fields to your recordset for storing data.
    With rs
        .Fields.Append "Row", adInteger
        .Fields.Append "Start", adDate
        .Fields.Append "End", adDate
        .Open
    End With

    lRow = 1

    ws1.Activate
    'Loop through and record what is in the columns
    Do While lRow <= ws1.UsedRange.Rows.count

        If ws1.Range("A" & lRow).Value <> "" Then
            rs.AddNew
            rs.Fields("Row").Value = lRow
            rs.Fields("Start").Value = ws1.Range("A" & lRow).Value
            rs.Fields("End").Value = ws1.Range("B" & lRow).Value
            rs.Update
        End If

        lRow = lRow + 1
        ws1.Range("A" & lRow).Activate
    Loop

    lRow = 1

    'Loop through and check for overlaps in the records.
    Do While lRow <= ws1.UsedRange.Rows.count

        iCount = 0

        If ws1.Range("A" & lRow).Value <> "" And ws1.Range("A" & lRow).Value <> "" Then

            'Check for those that started in the timespan
            rs.Filter = ""
            rs.Filter = "Start >= '" & ws1.Range("A" & lRow).Value & "' AND Start <='" & ws1.Range("B" & lRow).Value & "'"
            iCount = rs.RecordCount

            'Check for those that ended in the timespan
            rs.Filter = ""
            rs.Filter = "End >= '" & ws1.Range("A" & lRow).Value & "' AND End <='" & ws1.Range("B" & lRow).Value & "'"
            iCount = iCount + rs.RecordCount

            'Check for those that started before and ended after the current timespan.
            rs.Filter = ""
            rs.Filter = "Start <= '" & ws1.Range("A" & lRow).Value & "' AND End >='" & ws1.Range("B" & lRow).Value & "'"
            iCount = iCount + rs.RecordCount

            'Report the number. You minus three because the records that are the time will get counted each time
            ws1.Range("c" & lRow).Value = iCount - 3
        End If

        lRow = lRow + 1
    Loop

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多