同时重叠的日期时间数
Number of Simultaneous Overlapping datetime(s)
我有两列 date/time(s),需要找出它们在特定时间重叠的最大数量。
用例是这样的:这些是 phone 通话的开始和结束时间,我正在寻找同时通话的数量。
Column A Column B
8/06/15 00:17:00 8/06/15 00:19:00
8/09/15 00:20:00 8/09/15 00:30:00
8/09/15 00:25:00 8/09/15 00:40:00
8/09/15 00:35:00 8/09/15 00:50:00
8/09/15 00:45:00 8/09/15 00:55:00
8/09/15 00:46:00 8/09/15 00:52:00
预期结果:
Column A Column B Max Simultaneous
8/06/15 00:17:00 8/06/15 00:19:00 0
8/09/15 00:20:00 8/09/15 00:30:00 1
8/09/15 00:25:00 8/09/15 00:40:00 1
8/09/15 00:35:00 8/09/15 00:50:00 2
8/09/15 00:45:00 8/09/15 00:55:00 2
8/09/15 00:46:00 8/09/15 00:52:00 2
我正在尝试的公式是这样的:
=SUMPRODUCT((A:A006<=B2)*(B:B006>=A2))
这个问题是,它会计算重叠的总数,即使它们在任何一点都不重叠 - 大大增加了预期的结果。
我之前问的问题很烂:
我最终制作了一个 Sub 而不是 UDF。您可以在例程第二行的方括号中设置 phone 调用日志的范围。我只用你的六行样本数据测试过它。它将计算最大行数并将它们写入日志右侧的一列(示例中的 C 列)。
Public Sub MaxLines()
Dim c&, i&, j&, k&, min_#, max_#, n&, v, w&(), vOut, r As Range
Set r = [a2:b7]
v = r
With Application
min_ = .Min(.Index(r, 0, 1))
max_ = .Max(.Index(r, 0, 2))
End With
n = (max_ - min_) * 1440
ReDim vOut(1 To UBound(v), 1 To 1)
ReDim w(1 To n + 1)
For i = 1 To UBound(v)
k = (v(i, 1) - min_) * 1440 + 1
c = CLng((v(i, 2) - v(i, 1)) * 1440)
For j = 0 To c
w(j + k) = w(j + k) + 1
Next
Next
For i = 1 To UBound(v)
k = (v(i, 1) - min_) * 1440 + 1
c = CLng((v(i, 2) - v(i, 1)) * 1440)
max_ = 0
For j = 0 To c
If w(j + k) > max_ Then max_ = w(j + k)
Next
vOut(i, 1) = max_ - 1
Next
r.Resize(, 1).Offset(, 2) = vOut
End Sub
我有两列 date/time(s),需要找出它们在特定时间重叠的最大数量。
用例是这样的:这些是 phone 通话的开始和结束时间,我正在寻找同时通话的数量。
Column A Column B
8/06/15 00:17:00 8/06/15 00:19:00
8/09/15 00:20:00 8/09/15 00:30:00
8/09/15 00:25:00 8/09/15 00:40:00
8/09/15 00:35:00 8/09/15 00:50:00
8/09/15 00:45:00 8/09/15 00:55:00
8/09/15 00:46:00 8/09/15 00:52:00
预期结果:
Column A Column B Max Simultaneous
8/06/15 00:17:00 8/06/15 00:19:00 0
8/09/15 00:20:00 8/09/15 00:30:00 1
8/09/15 00:25:00 8/09/15 00:40:00 1
8/09/15 00:35:00 8/09/15 00:50:00 2
8/09/15 00:45:00 8/09/15 00:55:00 2
8/09/15 00:46:00 8/09/15 00:52:00 2
我正在尝试的公式是这样的:
=SUMPRODUCT((A:A006<=B2)*(B:B006>=A2))
这个问题是,它会计算重叠的总数,即使它们在任何一点都不重叠 - 大大增加了预期的结果。
我之前问的问题很烂:
我最终制作了一个 Sub 而不是 UDF。您可以在例程第二行的方括号中设置 phone 调用日志的范围。我只用你的六行样本数据测试过它。它将计算最大行数并将它们写入日志右侧的一列(示例中的 C 列)。
Public Sub MaxLines()
Dim c&, i&, j&, k&, min_#, max_#, n&, v, w&(), vOut, r As Range
Set r = [a2:b7]
v = r
With Application
min_ = .Min(.Index(r, 0, 1))
max_ = .Max(.Index(r, 0, 2))
End With
n = (max_ - min_) * 1440
ReDim vOut(1 To UBound(v), 1 To 1)
ReDim w(1 To n + 1)
For i = 1 To UBound(v)
k = (v(i, 1) - min_) * 1440 + 1
c = CLng((v(i, 2) - v(i, 1)) * 1440)
For j = 0 To c
w(j + k) = w(j + k) + 1
Next
Next
For i = 1 To UBound(v)
k = (v(i, 1) - min_) * 1440 + 1
c = CLng((v(i, 2) - v(i, 1)) * 1440)
max_ = 0
For j = 0 To c
If w(j + k) > max_ Then max_ = w(j + k)
Next
vOut(i, 1) = max_ - 1
Next
r.Resize(, 1).Offset(, 2) = vOut
End Sub