我写了一个计算两次之间时差的程序。
它计算一定程度上的时间差(或适当的几个单元)。在few cells
之后,它会写garbage values to the rest of the cells
。
请帮帮我。 请参阅下面的代码。
Sub Average()
Dim LogIn As String
Dim LogOff As String
Dim Row As Integer
Dim Col As Integer
Dim InTime As Date
Dim OffTime As Date
Row = 1
Col = 2
While (Cells(Row, Col) <> "")
Workbooks("Log-In-Time.xlsm").Activate
InTime = Cells(Row, Col)
Workbooks("Log-Off-Time.xlsm").Activate
OffTime = Cells(Row, Col)
Workbooks("Log-In-Time.xlsm").Activate
Cells(Row, Col + 1) = CDate(OffTime) - CDate(InTime)'<- Without CDate also I have tried but output was same.
Row = Row + 1
Wend
End Sub
我的Log-In-Time.xls内容是,
OUTPUT
7/11/2013 11:35:41 AM 7:14:15 AM
7/15/2013 11:05:22 AM 10:03:00 AM
7/16/2013 9:58:25 AM 11:11:31 AM
7/17/2013 10:33:20 AM 10:39:25 AM
7/18/2013 11:10:33 AM 6:58:35 AM
7/19/2013 12:18:59 AM 7:18:09 PM <-----Here onwadrs
7/22/2013 11:58:26 AM 0.370185185
7/23/2013 11:27:14 AM 0.418645833
7/24/2013 10:59:36 AM 0.439953704
7/25/2013 11:20:16 AM 0.382650463
7/26/2013 11:09:14 AM 0.373171296
Log-Off-Time.xls内容是,
7/11/2013 6:49:56 PM
7/15/2013 9:08:22 PM
7/16/2013 9:09:56 PM
7/17/2013 9:12:45 PM
7/18/2013 6:09:08 PM
7/19/2013 7:37:08 PM
7/22/2013 8:51:30 PM
7/23/2013 9:30:05 PM
7/24/2013 9:33:08 PM
7/25/2013 8:31:17 PM
7/26/2013 8:06:36 PM
答案 0 :(得分:0)
终于得到了答案......
刚刚添加了以下代码。
Dim Diff As Date
Diff = CDate(OffTime) - CDate(InTime)
找到下面的完整代码。
Sub Average()
Dim LogIn As String
Dim LogOff As String
Dim Diff As Date
Dim Row As Integer
Dim Col As Integer
Dim InTime As Date
Dim OffTime As Date
Row = 1
Col = 2
While (Cells(Row, Col) <> "")
Workbooks("Log-In-Time.xlsm").Activate
InTime = Cells(Row, Col)
Workbooks("Log-Off-Time.xlsm").Activate
OffTime = Cells(Row, Col)
Workbooks("Log-In-Time.xlsm").Activate
Diff = CDate(OffTime) - CDate(InTime)
Cells(Row, Col + 1) = Diff
Row = Row + 1
Wend
End Sub