简体   繁体   English

如何使用 VBA 计算 Excel 中差异格式的时间差

[英]How to Calculate time difference in difference format in Excel using VBA

I have 2 columns with dates in different format.我有 2 列不同格式的日期。

在此处输入图像描述

As you can see one column is in dd/mm/yyyy format and other is in mm/dd/yyyy format.如您所见,一列采用 dd/mm/yyyy 格式,另一列采用 mm/dd/yyyy 格式。

I want to find the time difference among the two.我想找出两者之间的时差。 Also, one column is in Date Format and other is in General Format.此外,一列采用日期格式,另一列采用通用格式。

I wrote a function in VB to calculate the difference like this:我在VB中写了一个function来计算这样的差异:

 Function caclulateDifference(pDate1 As Date, pDate2 As Date) As Long

   TestDates = DateDiff("d", pDate1, pDate2)

 End Function

And I Call this in Excel using =caclulateDifference(b2,a2) However,我使用 =caclulateDifference(b2,a2) 在 Excel 中调用它但是,

pDate1 is coming as 10/15/19 11:38:38 AM, but pDate2 appears as 10/19/15 9:09:23 AM and then difference is coming as 0. pDate1 将在 10/15/19 11:38:38 AM 出现,但 pDate2 显示为 10/19/15 9:09:23 AM,然后差异为 0。

I want difference in day, hour and minutes.我想要日、小时和分钟的差异。 What am I doing wrong?我究竟做错了什么?

You are getting 0 because there are 0 days between those values, PLUS, you never assign the Function a value to return anything other than the default value for Long .您得到 0 是因为这些值之间有 0 天,另外,您永远不会为 Function 分配一个值来返回Long的默认值以外的任何值。

Try it like this:试试这样:

 Function caclulateDifference(pDate1 As Date, pDate2 As Date) As Long

   caclulateDifference = DateDiff("d", pDate1, pDate2)

 End Function

You will still get 0, but in this example, that's what you are supposed to get.你仍然会得到 0,但在这个例子中,这就是你应该得到的。 Change the function to this, and then look at your debug window to see that the dates are getting formatted properly:将 function 更改为此,然后查看您的调试 window 以查看日期格式是否正确:

Public Function caclulateDifference(pDate1 As Date, pDate2 As Date) As Long

   Debug.Print pDate1
   Debug.Print pDate2

   caclulateDifference = DateDiff("d", pDate1, pDate2)

 End Function

Then test it using date values that are on different days to get something other than 0.然后使用不同日期的日期值对其进行测试,以获得 0 以外的值。

Now if you want it in more detail than days as you say, you need to return a value other than Long (might have milliseconds in the values too) and use a different argument in your DateDiff like this:现在,如果您希望它比您所说的天数更详细,则需要返回Long以外的值(值中也可能包含毫秒)并在DateDiff中使用不同的参数,如下所示:

Public Function caclulateDifference(pDate1 As Date, pDate2 As Date) As Double

   caclulateDifference = DateDiff("s", pDate1, pDate2)

End Function

Then convert the number of seconds into the number of days, hours, minutes, and seconds.然后将秒数转换为天数、小时数、分钟数和秒数。

Convert seconds to days, hours, minutes, and seconds 将秒转换为天、小时、分钟和秒

If you dont want the order to matter, and always get a positive number, use Abs (absolute value) function as well, like this:如果您不希望顺序重要,并且总是得到一个正数,也可以使用Abs (绝对值)function,如下所示:

Public Function caclulateDifference(pDate1 As Date, pDate2 As Date) As Double

   caclulateDifference = Abs(DateDiff("s", pDate1, pDate2))

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM