VBA 中的时差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17885006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:26:17  来源:igfitidea点击:

Time Difference in VBA

excelvbaexcel-vbascriptingvb6

提问by Rasmi Ranjan Nayak

I have written a program which calculates the time difference between two times. It calculates the time difference between upto some extent (or few cells appropriately). After few cellsit writes garbage values to the rest of the cells.

我写了一个程序来计算两次之间的时间差。它计算在一定程度上(或适当的少数细胞)之间的时间差。后few cells写入garbage values to the rest of the cells

Please help me. See the code below.

请帮我。请参阅下面的代码。

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

My Log-In-Time.xls content is,

我的 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 contents are,

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

回答by Rasmi Ranjan Nayak

Finally got the answer...

终于有答案了...

Just added the code below.

刚刚添加了下面的代码。

Dim Diff As Date

Dim Diff As Date

Diff = CDate(OffTime) - CDate(InTime)

Diff = CDate(OffTime) - CDate(InTime)

Find the complete code below.

在下面找到完整的代码。

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