VBA Convert date to week number

前端 未结 6 411
渐次进展
渐次进展 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:22

    So, this is my final and working perfectly version

    Public Function IsoWeekNumber(d As Date) As String
    
        Dim kwtemp As String
    
        kwtemp = DatePart("ww", d, vbMonday, vbFirstFourDays)
    
        If Len(kwtemp) = 1 Then kwtemp = "0" & kwtemp
    
        IsoWeekNumber = kwtemp
    
    End Function
    
    If Application.International(xlMDY) = True Then
    
    HeadlineTemp = Mid(VRHeadline, InStr(VRHeadline, "[") + 1, 10)
    HeadlineTemp = Replace(HeadlineTemp, ".", "/")
    HeadlineTemp = Mid(HeadlineTemp, 4, 3) & Left(HeadlineTemp, 2) & Right(HeadlineTemp, 5)
    VRFirstKW = CDate(HeadlineTemp)
    HeadlineTempEndKW = Mid(VRHeadline, InStr(VRHeadline, "]") - 10, 10)
    HeadlineTempEndKW = Replace(HeadlineTempEndKW, ".", "/")
    HeadlineTempEndKW = Mid(HeadlineTempEndKW, 4, 3) & Left(HeadlineTempEndKW, 2) & Right(HeadlineTempEndKW, 5)
    VREndKW = CDate(HeadlineTempEndKW)
    VRKW = "KW" & IsoWeekNumber(VRFirstKW) & "-" & IsoWeekNumber(VREndKW) & "/" & Year(VREndKW)
    
        Else 'don't switch position of the month with days
    
        HeadlineTemp = Mid(VRHeadline, InStr(VRHeadline, "[") + 1, 10)
        HeadlineTemp = Replace(HeadlineTemp, ".", "/")
        VRFirstKW = CDate(HeadlineTemp)
        HeadlineTempEndKW = Mid(VRHeadline, InStr(VRHeadline, "]") - 10, 10)
        HeadlineTempEndKW = Replace(HeadlineTempEndKW, ".", "/")
        VREndKW = CDate(HeadlineTempEndKW)
        VRKW = "KW" & IsoWeekNumber(VRFirstKW) & "-" & IsoWeekNumber(VREndKW) & "/" & Year(VREndKW)
    

提交回复
热议问题