vba convert week number (and year) to date?

后端 未结 3 555
渐次进展
渐次进展 2021-01-05 01:00

I have a week number in Cell C13 and a year in Cell C14.

I am using the below formula to convert this into the Thursday of

3条回答
  •  粉色の甜心
    2021-01-05 01:53

    As an additional option, for those using the ISO weeknumber system, where Week 1 of the year is the first week of the year containing four days (or the week that includes the first Thursday of the calendar year, and the first day of the week is Sunday.

    • DOW is an optional argument representing the day of the week. It will default to Thursday and, since we are using the ISO weeknumber system, should always fall within the current year.

    Option Explicit
    Function WNtoDate(WN As Long, YR As Long, Optional DOW As Long = 5) As Date
        'DOW:  1=SUN, 2=MON, etc
        Dim DY1 As Date
        Dim Wk1DT1 As Date
        Dim I As Long
    
    DY1 = DateSerial(YR, 1, 1)
    'Use ISO weeknumber system
    I = DatePart("ww", DY1, vbSunday, vbFirstFourDays)
    
    'Sunday of Week 1
    Wk1DT1 = DateAdd("d", -Weekday(DY1), DY1 + 1 + IIf(I > 1, 7, 0))
    
    WNtoDate = DateAdd("ww", WN - 1, Wk1DT1) + DOW - 1
    
    End Function
    

提交回复
热议问题