重叠日期时间数

时间:2015-09-09 18:14:52

标签: excel vba excel-vba date date-range

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

用例是这样的:这些是​​电话呼叫的开始和结束时间,我希望找到同时呼叫的数量。

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

由于您的标签中有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