求助excel vba 算时间差

在excel中有一开始时间和一结束时间 (如开始时间 2010-10-8 10:21:19 结束时间:2010-10-10 23:59:18 )现有计算这中间的时间差,且每天23:00到次日8:00的时间不计算在内(从2010-10-10 22:00:00到2010-10-10 23:55:00,中间时间差为3600秒;从2010-10-10 22:00:00到2010-10-11 7:59:59中间时间差也为3600秒;从2010-10-10 22:00:00到2010-10-11 9:00:00中间时间差为7200秒;从2010-10-11 7:00:00到2010-10-11 9:00:00中间时间差也为3600秒)

请高手写个vba程序,现有大量数据要计算,头痛啊,请好心人帮帮我啦,先谢谢啦!!!

Sub Macro1()

Dim i As Integer
Dim Date_1(0 To 2) As Variant
Dim Time_1(0 To 2) As Variant
Dim Date_2(0 To 2) As Variant
Dim Time_2(0 To 2) As Variant

For i = 1 To ActiveSheet.UsedRange.Rows.Count Step 1
Date_1(0) = Split(Split(ActiveSheet.Cells(i, 1).Value, " ")(0), "-")(0)
Time_1(0) = Split(Split(ActiveSheet.Cells(i, 1).Value, " ")(1), ":")(0)
Date_2(0) = Split(Split(ActiveSheet.Cells(i, 2).Value, " ")(0), "-")(0)
Time_2(0) = Split(Split(ActiveSheet.Cells(i, 2).Value, " ")(1), ":")(0)
Date_1(1) = Split(Split(ActiveSheet.Cells(i, 1).Value, " ")(0), "-")(1)
Time_1(1) = Split(Split(ActiveSheet.Cells(i, 1).Value, " ")(1), ":")(1)
Date_2(1) = Split(Split(ActiveSheet.Cells(i, 2).Value, " ")(0), "-")(1)
Time_2(1) = Split(Split(ActiveSheet.Cells(i, 2).Value, " ")(1), ":")(1)
Date_1(2) = Split(Split(ActiveSheet.Cells(i, 1).Value, " ")(0), "-")(2)
Time_1(2) = Split(Split(ActiveSheet.Cells(i, 1).Value, " ")(1), ":")(2)
Date_2(2) = Split(Split(ActiveSheet.Cells(i, 2).Value, " ")(0), "-")(2)
Time_2(2) = Split(Split(ActiveSheet.Cells(i, 2).Value, " ")(1), ":")(2)
ActiveSheet.Cells(i, 3).Value = (Date_1(0) - Date_2(0)) & "年" & (Date_1(1) - Date_2(1)) & "月" _
& (Date_1(2) - Date_2(2)) & "日" & _
((Time_1(0) - Time_2(0)) * 3600 + (Time_1(1) - Time_2(1)) * 60 + (Time_1(2) - Time_2(2))) _
& "秒"
Next
End Sub
A列输入开始时间 (从A1开始)格式最好是
(2010/10/5 23:59:18 是“/”
2010-10-10 23:59:18 是“-”)
B列输入结束时间 (从B1开始)
插入个按钮链接宏 (Macro1)
点击按钮C列将是所要结果
试试吧
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-10-21
很简单。
hi我吧

excel vba 计算时间差 急!!T.T
1.45小时和1小时45分,可不一样呀。直接相减即可 在C2中输入:=B2-A2 然后把C2设置成时间格式 结果是:1:45 VBA Range("C2")=Format(Range("B2")-Range("A2") "h:mm")

excel vba 计算时间差的问题~在线求!!
另外,你用address时,单元格的表示应该用大写字母,否则不会有事件发生 Private Sub Worksheet_Change(ByVal Target As Range)If Target.Count > 1 Then Exit SubIf Target.Address = "$I$2" And [h2] <> "" And [i2] <> "" Then[j2] = Format(Abs([i2] - [h2]), "h.mm")End...

excel vba 怎么计算两个时间的差值 计算12:35:00和0:05:00 之间的差值...
直接减就是呗 Sub Test() Dim dt1, dt2 As Date dt1 = #12:35:00 PM# dt2 = #12:05:00 AM# Debug.Print CDate(dt1 - dt2)End Sub

vba我要算两个时间差是否大于八小时 例如:b1单元格时间08:48 c1
Dim d1 As Date Dim d2 As Date Dim MyH As Integer d1 = Excel.Application.ActiveWorkbook.Sheets("Sheet1").Cells(1, 2)d2 = Excel.Application.ActiveWorkbook.Sheets("Sheet1").Cells(1, 3)MyH = DateDiff("h", d1, d2)MsgBox MyH ...

谁教教我用excel怎么算出公司员工考勤时间差,加班时间,迟到时间 数据...
其实用公式或者VBA都能实现。以下是用公式实现的:计算公式:正班:=TEXT(VALUE(MID(D2,6,5))-VALUE(MID(D2,1,4))+VALUE(MID(D2,17,5))-VALUE(MID(D2,12,5)),"HH:MM")加班:=TEXT(VALUE(MID(D2,30,5))-VALUE(MID(D2,24,5)),"HH:MM")迟到\/早退:=VALUE("8:00")-E2 ...

如何用excel vba计算时间加减
VBA 的日期以日为基数。 加时要加 时数 \/ 24 加分要加 分数 \/ 60 \/ 24 加秒要加 秒数 \/ 60 \/ 60 \/ 24 试试执行下面的 vba Sub addtime() Dim xDate As Date '现在日期及时间 xDate = Now MsgBox "现在的日期时间是:" & Chr(13) & Chr(13) & xDate '假设加5小时30分 MsgBox...

求助excel vba 算时间差
Dim Date_1(0 To 2) As Variant Dim Time_1(0 To 2) As Variant Dim Date_2(0 To 2) As Variant Dim Time_2(0 To 2) As Variant For i = 1 To ActiveSheet.UsedRange.Rows.Count Step 1 Date_1(0) = Split(Split(ActiveSheet.Cells(i, 1).Value, " ")(0), "-")(0)Time...

excel如何计算不规律多行时间差值?
使用VBA,编一个简单的FOR语句,处理下逻辑,自动处理就好了。如果你每一簇中间只间隔了一个空白行,可以使用下面这个简单的宏(ALT+F11-右键 插入模块- 复制下面的代码- 运行):Sub test()Dim i, k As Integer k = 2 For i = 2 To 1000 '(取表格数据最后一行行数)If Range("a" & i) ...

如何在EXCEL的一个单元格内,统计几个时间段或者是叫时间差吧,就是为了...
用万能的vba写一个自定义函数可以实现。编制代码如下:Function ddjx(s) As String 'Dim sarr()Dim jgarr(1 To 10, 1 To 4) '1-姓名 2-开始时间 3-结束时间 4-间隔时间 Dim kssj As Date Dim jssj As Date sarr = Split(s, ";")ss0 = sarr(0)ss0arr = Split(ss0, "|")myte...

excel如何使用vba函数计算指定间隔月后的日期?
今天小编为大家分享excel中使用vba计算指定间隔月后的日期方法,不会的朋友可以参考本文,希望能对大家有所帮助!步骤 1、首先我们打开一个工作样表作为例子。2、使用alt+f11组合快捷键进入vbe编辑器,插入一个新的模块,并在模块中输入以下代码:Option Explicit Sub ddt()Dim rq As Date Dim lx As ...

相似回答