
答案1
最簡單的想法(也遠離任何優化或程式碼品質):
1) 假設我們可以忽略期間的短暫休息(例如10:00-14:00 14:15-18:00 與10:00-18:00 相同)。我在變數中這樣做轉移透過第一次和最後一次進入細胞(並去除結腸)。
2)建立一個1到2400天的「分鐘」陣列(所以幾乎一半的陣列根本沒有被使用)
3)每「分鐘」計算有多少人在工作
我們將 1 加到班次開始位置 - 這樣我們就不會計算邊界重疊(例如,如果“A”在 12:00 開始且“B”在 12:00 結束,則這不會是重疊)
Function shiftOverlaps(r As Range) As Integer
Dim shiftStart As Integer
Dim shiftEnd As Integer
Dim shift As String
Dim wholeDay(1 To 2400) As Integer
shiftOverlaps = 0
For Each c In r
shift = Replace(Left(Trim(c.Value), 5) & " " & Right(Trim(c.Value), 5), ":", "")
If shift <> " " Then
shiftStart = Left(shift, 4) + 1
shiftEnd = Right(shift, 4)
For i = 1 To 2400
If i >= shiftStart And i <= shiftEnd Then
wholeDay(i) = wholeDay(i) + 1
If wholeDay(i) > shiftOverlaps Then
shiftOverlaps = wholeDay(i)
End If
End If
Next i
End If
Next c
shiftOverlaps = shiftOverlaps - 1
End Function