ジョブ シフトをリストした Excel スプレッドシートがあります...目的にとって重要な 4 つの列:
- 開始日
- 始まる時間
- 終了日
- 終了時間
このデータを考慮して、午前 7 時から午後7 時までの各シフトの時間数を抽出する必要があります。
留意すべき点がいくつかあります...
VBA 、分をあまり考慮していません。数分必要かどうかわかりません。
Dim i As Integer
Dim ShiftRange As Range
Dim dteStart As Date
Dim dteEnd As Date
Dim HourCount As Long
Dim MinCount As Long
Set ShiftRange = Sheet1.UsedRange
For i = 2 To ShiftRange.Rows.Count
HourCount = 0
dteStart = CDate(Cells(i, 1) + Cells(i, 2))
dteEnd = CDate(Cells(i, 3) + Cells(i, 4))
Do While dteStart <= dteEnd
If Hour(dteStart) >= 7 And Format(dteStart, "hh:mm") <= #7:00:00 PM# Then
HourCount = HourCount + 1
End If
dteStart = DateAdd("h", 1, dteStart)
Loop
MinCount = HourCount * 60
''Minutes
If CDate(Cells(i, 2)) >= #7:00:00 AM# And CDate(Cells(i, 2)) <= #7:00:00 PM# Then
MinCount = MinCount - Minute(CDate(Cells(i, 2)))
End If
If CDate(Cells(i, 4)) >= #7:00:00 AM# And CDate(Cells(i, 4)) <= #7:00:00 PM# Then
MinCount = MinCount + Minute(CDate(Cells(i, 4)))
End If
Cells(i, 6) = MinCount
Next
これは、A、B、C、およびDが日付と時刻を含む列であり、列Fが空であることを前提としています。
Remou ありがとうございました。
時間の一部を許可するように変更しました
Sub DayHours()
Dim i As Integer
Dim ShiftRange As Range
Dim dteStart As Date
Dim dteEnd As Date
Dim MinCount As Double
Set ShiftRange = Sheet1.UsedRange
For i = 3 To ShiftRange.Rows.Count
MinCount = 0
dteStart = CDate(Cells(i, 3) + Cells(i, 4))
dteEnd = CDate(Cells(i, 5) + Cells(i, 6))
Do While dteStart <= dteEnd
If Format(dteStart, "hh:mm") >= #7:00:00 AM# And Format(dteStart, "hh:mm") < #7:00:00 PM# Then
MinCount = MinCount + 1
End If
dteStart = DateAdd("n", 1, dteStart)
Loop
Cells(i, 10) = MinCount / 60
Next
End Sub