WEEK of year

User avatar
sal21
PlatinumLounger
Posts: 4373
Joined: 26 Apr 2010, 17:36

WEEK of year

Post by sal21 »

Is thsi corret to loop in year all weeks?

Code: Select all

Option Explicit
Private Sub Command2_Click()

Dim sDate As String
Dim I As Integer

    sDate = "01/01/" & "2011" 'Year(Date)
    
    If Day(CDate(sDate)) <> vbMonday Then
        sDate = DateAdd("D", vbMonday - Day(CDate(sDate)) - 1, CDate(sDate))
    End If
    
    For I = 0 To 52
        Debug.Print Format(DateAdd("WW", I, sDate), "DD/MM/YYYY")
        If Date > DateAdd("WW", I, sDate) And Date < DateAdd("WW", I + 1, sDate) Then
            '...............
        End If
    Next I

End Sub

User avatar
HansV
Administrator
Posts: 78631
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: WEEK of year

Post by HansV »

How do you want weeks to be defined?

a) Starting on January 1, January 8, January 15, ... (this is your present code)?
b) On the first Sunday of the year, and then the following Sundays?
c) On the Monday of the week that contains January 4?
d) Something else?
Best wishes,
Hans