简体   繁体   English

使用日期函数计算工作日

[英]Calculate weekdays with Date functions

Below is my SQL code to calculate the weekdays for the report. 下面是我的SQL代码,用于计算报告的工作日。 I am trying to get the same result with VBA Excel. 我正在尝试使用VBA Excel获得相同的结果。

set @end_date = DATEADD(DD,-DATEPART(dw,GETDATE())+1,GETDATE())
set @begin_date = DATEADD(DD,-6,@end_date)

I tried this: 我尝试了这个:

endDate = DateAdd("D", -DatePart("W", DateValue(Now)) + 1, "DateValue(Now)")
startDate = DateAdd("D", -6, "endDate")

However I can't get the result! 但是我无法得到结果!

I was able to get the same output if i added parenthesis after Now and remove the quotes from the last input of the DateAdd function and the endDate variable. 如果我在Now之后添加括号并从DateAdd函数的最后一个输入和endDate变量中删除引号,则能够获得相同的输出。 The quotes make the code think you are passing in a string instead of a function or variable. 引号使代码认为您正在传递字符串而不是函数或变量。

So your VBA would look like: 因此,您的VBA如下所示:

endDate = DateAdd("D", -DatePart("W", DateValue(Now())) + 1, DateValue(Now()))

startDate = DateAdd("D", -6, endDate)

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

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