ISO week number in VBScript or VBA

↘锁芯ラ 提交于 2020-02-24 11:29:27

问题


How can I get the ISO week number of some date in VBScript or VBA?


回答1:


First, note that:

  1. It is important to report the week year along with the week number, as the date's year could be different.
  2. Several Windows components contain a bug for some years' last Monday.

In my experience the simplest, clearest and most robust way to compute this is:

Sub WeekNum(someDate, isoWeekYear, isoWeekNumber, isoWeekDay)
  Dim nearestThursday
  isoWeekDay = WeekDay(someDate, vbMonday)
  nearestThursday = DateAdd("d", 4 - Int(isoWeekDay), someDate)
  isoWeekYear = Year(nearestThursday)
  isoWeekNumber = Int((nearestThursday - DateSerial(isoWeekYear, 1, 1)) / 7) + 1
End Sub

This also returns the ISO day of the week, counting from 1 for Mondays.




回答2:


Enter any date into A1 cell, then run following code...

Range("A2").FormulaR1C1 = "=INT((R1C-DATE(YEAR(R1C-WEEKDAY(R1C-1)+4),1,3)+WEEKDAY(DATE(YEAR(R1C-WEEKDAY(R1C-1)+4),1,3))+5)/7)"


来源:https://stackoverflow.com/questions/35010785/iso-week-number-in-vbscript-or-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!