-2

我写了一个程序来计算两次之间的时间差。它在一定程度上(或适当的几个单元格)计算时间差。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
4

1 回答 1

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
于 2013-07-26T15:26:36.230 回答