简体   繁体   中英

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.

But in excel(VBA and Sheet Formula) this is the week nr. 1 of 2016.

 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. 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 .

BTW, the function =WEEKNUM() and WorksheetFunction.Weeknum() work differently in the Excel versions, so you might have more control with a pure VBA solution.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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