poltergeist
Programmer
If you test the sub it will give you back the Weeknumber, First and last day of this week. But there is a bug in the DatePart-function and also in format. In the year in which 29 diciember is a monday like 2003 it give Weeknumber 53 and it must be Weeknumber 1.
Private Sub Command1_Click()
Dim WeekNum
Dim FirstDayOfWeek
Dim LastDayOfWeek
If IsDate(Text1.Text) Then
WeekNum = Format(Text1.Text, "ww", vbMonday)
FirstDayOfWeek = Format(((CDate(Text1.Text) - (Weekday(Text1.Text, vbMonday) - 1))), "c"
LastDayOfWeek = Format(CDate(FirstDayOfWeek) + 6, "c"
MsgBox "Week No" & Chr(9) & WeekNum & Chr(13) & _
"First day" & Chr(9) & FirstDayOfWeek & Chr(13) & _
"Last day" & Chr(9) & LastDayOfWeek
End If
End Sub
So there is e little solution:
Public Function WeeksInYear(Optional ByVal TestDate As Variant, _
Optional ByVal TestYear As Variant, _
Optional ByVal FirstDayOfWeek As VbDayOfWeek _
= vbUseSystemDayOfWeek, _
Optional ByVal FirstWeekOfYear As VbFirstWeekOfYear _
= vbUseSystem) As Integer
Dim nYear As Integer
Dim nDate As Date
Dim nWeek As Integer
Dim nWeekday As Integer
If IsMissing(TestDate) Then
If IsMissing(TestYear) Then
'Error
Else
nYear = TestYear
End If
Else
nYear = Year(TestDate)
End If
nDate = DateSerial(nYear, 12, 31)
Do
nWeek = DatePart("ww", nDate, FirstDayOfWeek, FirstWeekOfYear)
Select Case nWeek
Case 1
nDate = nDate - 1
Case 53
If DatePart("ww", nDate + 7, _
FirstDayOfWeek, FirstWeekOfYear) = 2 Then
WeeksInYear = 52
Else
WeeksInYear = 53
End If
Exit Function
Case 52
WeeksInYear = 52
Exit Function
End Select
Loop
End Function
peterguhl@yahoo.de
Private Sub Command1_Click()
Dim WeekNum
Dim FirstDayOfWeek
Dim LastDayOfWeek
If IsDate(Text1.Text) Then
WeekNum = Format(Text1.Text, "ww", vbMonday)
FirstDayOfWeek = Format(((CDate(Text1.Text) - (Weekday(Text1.Text, vbMonday) - 1))), "c"
LastDayOfWeek = Format(CDate(FirstDayOfWeek) + 6, "c"
MsgBox "Week No" & Chr(9) & WeekNum & Chr(13) & _
"First day" & Chr(9) & FirstDayOfWeek & Chr(13) & _
"Last day" & Chr(9) & LastDayOfWeek
End If
End Sub
So there is e little solution:
Public Function WeeksInYear(Optional ByVal TestDate As Variant, _
Optional ByVal TestYear As Variant, _
Optional ByVal FirstDayOfWeek As VbDayOfWeek _
= vbUseSystemDayOfWeek, _
Optional ByVal FirstWeekOfYear As VbFirstWeekOfYear _
= vbUseSystem) As Integer
Dim nYear As Integer
Dim nDate As Date
Dim nWeek As Integer
Dim nWeekday As Integer
If IsMissing(TestDate) Then
If IsMissing(TestYear) Then
'Error
Else
nYear = TestYear
End If
Else
nYear = Year(TestDate)
End If
nDate = DateSerial(nYear, 12, 31)
Do
nWeek = DatePart("ww", nDate, FirstDayOfWeek, FirstWeekOfYear)
Select Case nWeek
Case 1
nDate = nDate - 1
Case 53
If DatePart("ww", nDate + 7, _
FirstDayOfWeek, FirstWeekOfYear) = 2 Then
WeeksInYear = 52
Else
WeeksInYear = 53
End If
Exit Function
Case 52
WeeksInYear = 52
Exit Function
End Select
Loop
End Function
peterguhl@yahoo.de