Vba 重叠日期时间的数目

Vba 重叠日期时间的数目,vba,excel,date,date-range,Vba,Excel,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

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

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

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))
我还想提及类似的问题,这些问题并不能完全回答我的需要,但却帮助我走到了这一步:


我想你只需要从你现在的公式中减去一个:

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

既然您的标签中有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

你需要寻找那些完全重叠的吗?所以他们先开始后结束,是的。任何重叠的东西。基本上,有多少是同时发生的。答案被更新以捕获完全重叠。但我似乎无法使它工作,所有结果返回为0。它在这里工作。它假设您的电话通话时间从第2行开始,一直到第35006行。你必须确保没有一个条目是真正的文本而不是真实的数字。如果你愿意,我可以提供帮助。把文件寄给我,我会很快把它整理好的。我的电子邮件地址是:丹尼尔。ferry@gmail.comI现在认识到我问这个问题的方式有一个缺陷。如果它说2,它应该只说1,因为一次只有一个重叠。。是的,有两条记录重叠,但它们没有交叉。请提出一个新问题。