简体   繁体   中英

VBA get start date from week number of the year

I would like to retrieve the date of the first day of a week (week starts on Monday) knowing the year and the number of the week. (52 weeks in 1 year)

Dim nYear As Integer, _
    nWeek, As Integer
    startDate As Date

    nYear = 2018
    nWeek= 1


'startDate = ?

Expected result : 01/01/2018

Is there a simple & dynamic way to do so?

Thank you

Dim nTrimester As Integer, _
    nYear As Integer, _
    nWeek As Integer, _
    wd As Integer, _
    startDate As Date, _
    inputDate As Date

nYear = 2019
nWeek = 1    

inputDate = DateSerial(nYear, 1, 1)
inputDate = DateAdd("ww", nWeek - 1, inputDate)
wd = Weekday(inputDate, vbMonday)   

startDate = DateAdd("d", 1 - wd, inputDate)

I found a solution :

startDate = DateAdd("ww", nWeek, DateSerial(Year(Date), 1, 1))

Thank you to everyone that wanted to help!

If you have something that works the same or better, feel free to add it

尝试此操作,您必须考虑一年中第一天的星期几。

    startDate = DateAdd("ww", IIF(WeekDay(DateSerial(Year(Date), 1, 1))>4, nWeek,nWeek-1), DateSerial(Year(Date), 1, 1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM