我有一个excel文件,它显示了date1,date2以及它们之间的区别,格式为[h]:mm
但我无法总结这些日期的差异。它加起来为零。 我请求帮助,我找不到任何答案
T1 T2 Variance
2014/04/16 12:00 2014/04/15 14:00 =IF(B2-A2<0,"-"&TEXT(ABS(B2-A2),"[h]:mm"),TEXT(ABS(B2-A2),"[h]:mm"))
2014/05/16 12:00 2014/04/15 16:10 =IF(B3-A3<0,"-"&TEXT(ABS(B3-A3),"[h]:mm"),TEXT(ABS(B3-A3),"[h]:mm"))
2014/04/15 15:00 2014/04/15 16:00 =IF(B4-A4<0,"-"&TEXT(ABS(B4-A4),"[h]:mm"),TEXT(ABS(B4-A4),"[h]:mm"))
2014/04/15 15:00 2014/05/15 16:00 =IF(B5-A5<0,"-"&TEXT(ABS(B5-A5),"[h]:mm"),TEXT(ABS(B5-A5),"[h]:mm"))
答案 0 :(得分:1)
首先在标准模块中输入以下 UDF :
Public Function Addtime(rIn As Range) As String
Dim c As String, m As String, r As Range, v As String
Dim ttime As Long, zum As Long, b As Boolean
c = ":"
m = "-"
zum = 0
For Each r In rIn
v = r.Text
If InStr(v, c) > 0 And v <> "" Then
ary = Split(Replace(v, m, ""), c)
b = (Left(v, 1) = m)
ttime = 60 * CLng(ary(0)) + CLng(ary(1))
If b Then
zum = zum - ttime
Else
zum = zum + ttime
End If
End If
Next r
abszum = Abs(zum)
While abszum > 59
hours = hours + 1
abszum = abszum - 60
Wend
minutes = abszum
Addtime = CStr(hours) & c & CStr(minutes)
If zum < 0 Then
Addtime = m & Addtime
End If
End Function
然后在 C6 中输入:
=Addtime(C2:C5)
这应显示:-40:50
用户定义函数(UDF)非常易于安装和使用:
如果保存工作簿,UDF将随之保存。 如果您在2003年之后使用的是Excel版本,则必须保存 该文件为.xlsm而不是.xlsx
删除UDF:
从Excel使用UDF:
= myfunction的(A1)
要了解有关宏的更多信息,请参阅:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
和
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
和
http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx
有关UDF的详细信息
必须启用宏才能使其生效!