In VBA I want to convert a date as 03/11/2017(DD/MM/YYYY) into the week number for that date.
Until now I have the following code:
'geting the date out of the string
HeadlineTemp = Mid(VRHeadline, InStr(VRHeadline, "[") + 1, 10)
'switch "." to "/"
HeadlineTemp = Replace(HeadlineTemp, ".", "/")
'convert to a date
FristVRFirstKW = CDate(HeadlineTemp)
Now, I need a function to convert that date into the week number of the year. First week day is Monday.
WeekdayName( number, [abbreviate], [firstdayofweek] ) WeekdayName(2)
Result: 'Monday'WeekdayName(2, TRUE)
Result: 'Mon'WeekdayName(2, TRUE, vbMonday)
Result: 'Mon'To make the Week Number with Monday as a first day, use the following:
Using VBA, to convert a date into an isoWeeknumber, you merely need the
DatePart
function (where DT is the date of interest):If you want to use other definitions than that specified in ISO 8601, investigate some of the other options for
FirstDayOfWeek
andFirstWeekOfYear
NOTE
As pointed out by @Mike85, there is a bug in
DatePart
(and also in theFormat
) function wherein Monday may be erroneously given a weeknumber of 53 when it should be 1.There are a variety of workarounds.
In Excel 2013+ (Excel for Mac 2011+) you can use for the ISO Weeknumber:
For earlier versions, you can test the Monday and adjust it if necessary, or you can write a separate routine.
Calculate ISO year using datepart with bugs workaround:
So, this is my final and working perfectly version
Be carefull when it comes to week numbers as there are different definitions around. The Excel definition differs from the ISO definition. To get the ISO weeknumber use (copied From http://www.rondebruin.nl/win/s8/win001.htm)