简体   繁体   English

VBA:DateDiff 不返回确切的工作日

[英]VBA: DateDiff does not return exact working days

my general manager asked me to track the working days of our company's current projects that is recorded in an excel file and he wanted to me to make a new column for the remaining working days.我的总经理让我跟踪记录在excel文件中的我们公司当前项目的工作日,他希望我为剩余的工作日创建一个新列。 I tried to make below code a function from the net to get the range of the days:我试图让下面的代码成为一个来自网络的函数来获取天数的范围:

Function RDays(pDate1 As Date, pDate2 As Date) As Long
    RDays = DateDiff("d", pDate1, pDate2)
End Function

but the results that i only get a value of 31 as a maximum days and when the value is greater than 31, the value returns to 1..但结果我只得到 31 作为最大天数的值,当该值大于 31 时,该值返回到 1..

What would be the problem of this code?这段代码会有什么问题?

The excel in-built NETWORKDAYS(start_date, end_date, [holidays]) function will work perfectly well for returning the number of working days between two dates. excel 内置的NETWORKDAYS(start_date, end_date, [holidays])函数非常适合返回两个日期之间的工作日数。

More info: https://support.office.com/en-ie/article/networkdays-function-48e717bf-a7a3-495f-969e-5005e3eb18e7更多信息: https : //support.office.com/en-ie/article/networkdays-function-48e717bf-a7a3-495f-969e-5005e3eb18e7

Well, for one thing, it's Datedif in Excel, not Datediff, which is SQL Server.嗯,一方面,它是 Excel 中的 Datediff,而不是 SQL Server 中的 Datediff。

Here is the general function, with arguments:这是带参数的通用函数:

=DATEDIF (start_date, end_date, unit)

Here are the arguments, in units:以下是参数,单位:

Unit    Return value
"Y"     Difference in complete years
"M"     Difference in complete months
"D"     Difference in days
"MD"    Difference in days, ignoring months and years
"YM"    Difference in months, ignoring days and years
"YD"    Difference in days, ignoring years

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

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