VBA Convert date to week number

前端 未结 6 409
渐次进展
渐次进展 2021-01-12 12:01

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 d         


        
6条回答
  •  醉梦人生
    2021-01-12 12:44

    Calculate ISO year using datepart with bugs workaround:

    'Test 2007-12-31 should return W01Y2008
    
    myDate = "2007-12-31"
    
    ISOWeek = DatePart("ww", myDate, vbMonday, vbFirstFourDays)
    Week1 = DatePart("ww", myDate, vbMonday, vbFirstFourDays)
    Week2 = DatePart("ww", DateAdd("d", 7, myDate), vbMonday, vbFirstFourDays)
    ISOYear = DatePart("yyyy", myDate, vbMonday, vbFirstFourDays)
    Year1 = DatePart("yyyy", myDate, vbMonday, vbFirstFourDays)
    Year2 = DatePart("yyyy", DateAdd("d", 7, myDate), vbMonday, vbFirstFourDays)
    
    If ISOWeek = 53 And DatePart("ww", DateAdd("d", 7, myDate), vbMonday, vbFirstFourDays) = 2 Then
       ISOWeek = 1
    End If
    
    if ISOWeek = 1 And DatePart("yyyy", DateAdd("d", 7, myDate), vbMonday, vbFirstFourDays) > ISOYear Then
     ISOYear = ISOYear + 1
    End If
    
    MsgBox("W" & ISOWeek & "Y" & ISOYear)
    
    ' Result in W01Y2008
    

提交回复
热议问题