简体   繁体   English

Excel:VBA和公式表中的WeekNum错误

[英]Excel: WeekNum error in VBA and Formula Sheet

For each calendar the number of week of days 01-02-03/jan/2016 is 53th weeks. 对于每个日历,星期几01-02-03 / jan / 2016是第53周。

But in excel(VBA and Sheet Formula) this is the week nr. 但是在excel(VBA和工作表公式)中,这是星期nr。 1 of 2016. 2016年1月1日。

 cellValue = "01/01/2016"
 res = WorksheetFunction.WeekNum(cellValue, 2)

This is serious mistake or correct ? 这是严重的错误还是正确的?

There is a patch for this problem ? 是否有针对此问题的补丁?

Looks like you're using the ISO definition of week number. 看起来您正在使用周数的ISO定义。 If so then you'll need to change the line to: 如果是这样,那么您需要将行更改为:

res = DatePart("ww", d, vbMonday, vbFirstFourDays)

For a really good explanation of VBA for weeknum calculations, have a look at http://www.cpearson.com/excel/WeekNumbers.aspx . 有关VBA用于周数计算的真正很好的解释,请访问http://www.cpearson.com/excel/WeekNumbers.aspx

BTW, the function =WEEKNUM() and WorksheetFunction.Weeknum() work differently in the Excel versions, so you might have more control with a pure VBA solution. 顺便说一句,在Excel版本中,函数=WEEKNUM()WorksheetFunction.Weeknum()工作方式有所不同,因此使用纯VBA解决方案可能具有更多控制权。

您应该使用21而不是2作为第二个参数。

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

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