【发布时间】:2017-09-29 16:49:39
【问题描述】:
我在一家运行在线客户服务聊天的公司工作。每个员工一次最多可以进行两次聊天。我需要找出每个员工每天花在“双重聊天”上的总时间。我打算通过将每个聊天的重叠时间相加来做到这一点。但是,由于多个聊天可以同时发生,并且聊天的时间长短没有限制,因此单个长聊天可能会与几个较短的聊天重叠。那就是我遇到麻烦的地方。我发现这段代码可以找到重叠的时间:
Sub intersectHours()
Dim a As Long, i As Long, j As Long, rng As Range, ohrs As Double
With Worksheets("Sheet1")
For i = 2 To .Cells(Rows.Count, "E").End(xlUp).Row
ohrs = 0: Set rng = Nothing
For j = 2 To .Cells(Rows.Count, "E").End(xlUp).Row
If j <> i And Not Intersect(.Range(Hour(.Cells(i, "D").Value2) & ":" & Hour(.Cells(i, "E").Value2) - 1), _
.Range(Hour(.Cells(j, "D").Value2) & ":" & Hour(.Cells(j, "E").Value2) - 1)) Is Nothing Then
If rng Is Nothing Then
Set rng = Intersect(.Range(Hour(.Cells(i, "D").Value2) & ":" & Hour(.Cells(i, "E").Value2) - 1), _
.Range(Hour(.Cells(j, "D").Value2) & ":" & Hour(.Cells(j, "E").Value2) - 1))
Else
Set rng = Union(rng, Intersect(.Range(Hour(.Cells(i, "D").Value2) & ":" & Hour(.Cells(i, "E").Value2) - 1), _
.Range(Hour(.Cells(j, "D").Value2) & ":" & Hour(.Cells(j, "E").Value2) - 1)))
End If
End If
Next j
If Not rng Is Nothing Then
For a = 1 To rng.Areas.Count
ohrs = ohrs + TimeSerial(rng.Areas(a).Rows.Count, 0, 0)
Next a
End If
'.Cells(i, 6).NumberFormat = "[h]:mm:ss"
.Cells(i, 6) = ohrs
Next i
End With
End Sub
但是,我的数据是 h:mm:ss 格式,因此重叠时间输出应该以相同的方式测量。上面的代码只输出几个小时,我对 VBA 的了解不够,无法适应我的目的。非常感谢任何帮助!
【问题讨论】: