我有以下列表:
ID In Out
A 23.03.2018 08:16:14 23.03.2018 13:56:03
B 23.03.2018 11:16:14 23.03.2018 13:56:03
我必须创建类似这样的内容:
ID In
A 23.03.2018 09:00:00
A 23.03.2018 10:00:00
A 23.03.2018 11:00:00
A 23.03.2018 12:00:00
B 23.03.2018 12:00:00
“输入”和“输出”列分别包含一个日期和一个小时。我需要做的是查看“入”和“出”的小时数,并计算它们之间的小时数。像第二张表一样,每个计算的小时数必须保存在一行中。例如,它可以是另一个表Result。例如,如果In从10:01:00开始,我将从11开始计数。您知道我该怎么做吗?我尝试过使用数据透视表,但我认为用这种方式无法计数。谢谢!
答案 0 :(得分:0)
尝试一下:
Sub TimeSheet()
Dim timeEntries As Range, entry As Range, startTime As Integer, endTime As Integer, hr As Integer, lastRow As Integer
Set timeEntries = Worksheets("Input").Range("A2:A3")
For Each entry In timeEntries
startTime = GetHour(entry.Offset(0, 1), "IN")
endTime = GetHour(entry.Offset(0, 2), "OUT")
For hr = startTime To endTime
With Worksheets("Output")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
.Range("A" & lastRow) = entry
.Range("B" & lastRow) = DateValue(entry.Offset(0, 1)) & " " & TimeSerial(hr, 0, 0)
End With
Next hr
Next entry
End Sub
Function GetHour(t As Date, stamp As String) As Date
Dim result As Date
If stamp = "IN" Then
If Minute(t) = 0 Then
result = Hour(t)
Else
result = Hour(DateAdd("h", 1, t))
End If
Else
If Minute(t) = 0 Then
result = Hour(t)
Else
result = Hour(DateAdd("h", -1, t))
End If
End If
GetHour = result
End Function
注释
Input
的{{1}}和In
数据从Out
开始(第一行作为标题)A2
的摘要表从Output
开始(第一行作为标题)A2
函数根据您的逻辑获取开始时间和结束时间