重叠日期时间的数量
Number of Overlapping datetime(s)
我有两列 date/time(s),需要找出它们在特定时间有多少重叠。
用例是这样的:这些是 phone 通话的开始和结束时间,我正在寻找同时通话的数量。
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:A006<=B2)*(B:B006>=A2))
我还想参考类似的问题,这些问题并不能完全回答我在这里的需要,但帮助我走到了这一步:
Find number of concurrent, overlapping, date ranges
我认为你只需要从当前公式中减去一个:
=SUMPRODUCT((A:A006<=B2)*(B:B006>=A2))-1
既然你的标签中有 VBA,下面是你如何做到的。
在您 VBA IDE 中,转到工具菜单并选择参考。 Select“Microsoft 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
我有两列 date/time(s),需要找出它们在特定时间有多少重叠。
用例是这样的:这些是 phone 通话的开始和结束时间,我正在寻找同时通话的数量。
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:A006<=B2)*(B:B006>=A2))
我还想参考类似的问题,这些问题并不能完全回答我在这里的需要,但帮助我走到了这一步:
Find number of concurrent, overlapping, date ranges
我认为你只需要从当前公式中减去一个:
=SUMPRODUCT((A:A006<=B2)*(B:B006>=A2))-1
既然你的标签中有 VBA,下面是你如何做到的。
在您 VBA IDE 中,转到工具菜单并选择参考。 Select“Microsoft 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