Schon fast vergessen noch zu posten. Ich glaub ich hab's - siehe Code unten.
Zurückgegeben wird ein Datum, welches der Montag der Kalenderwoche ist.
Ein mulmiges Gefühl habe ich noch beim Jahreswechsel (KW 52/53/01). Nach den ersten Tests klappt es aber.
Quelle war übrigens eine Excel-Formel:
Kalenderwoche eingeben und Zeitraum erhalten nach DIN
In A1 ist die Kalenderwoche.
In A2 die vierstellige Jahreszahl
In A3 (Hilfszelle) steht:
=DATUM(A2;1;1)+(A1-WENN(WOCHENTAG(DATUM(A2;1;1);2)>4;0;1))*7
Der Montag der KW steht in A5:
=A3-WOCHENTAG(A3;2)+1
| Public Function GetWeekMonday(iInputWeek As Integer, iInputYear As Integer) As Variant |
| |
| On Error Goto ERRORHANDLER |
| |
| Dim iDaysInYear As Integer |
| Dim iWeekday As Integer |
| Dim iWeekday2 As Integer |
| 'Variants of DataType 7 (Date/Time) ---> |
| Dim vFirstJan As Variant |
| Dim vDateRef As Variant |
| Dim vDateMonday As Variant |
| '< |
| |
| '--> Errorhandling |
| If iInputWeek > 53 Then |
| Error 1001, "Wrong calendar week '" & Cstr(iInputWeek) & "'. Must not be > 53" |
| End If |
| If iInputWeek < 0 Then |
| Error 1002, "Wrong calendar week '" & Cstr(iInputWeek) & "'. Must not be < 0" |
| End If |
| If Len( Cstr(iInputYear) ) <> 4 Then |
| Error 1003, "Wrong year '" & Cstr(iInputYear) & "'. It must have 4 digits" |
| End If |
| '< |
| |
| 'The first day of the year |
| vFirstJan = Datevalue("01/01/" + Cstr(iInputYear)) |
| |
| 'Days in the year |
| iWeekday = CalculateIsoWeekday( vFirstJan ) |
| If iWeekday > 3 Then |
| iDaysInYear = iInputWeek * 7 |
| Else |
| iDaysInYear = (iInputWeek-1) * 7 |
| End If |
| |
| 'We create a reference date in the current year |
| vDateRef = vFirstJan + iDaysInYear |
| 'We need the monday |
| iWeekday2 = CalculateIsoWeekday( vDateRef ) + 1 |
| If iWeekday2 = 1 Then |
| vDateMonday = vDateRef |
| Else |
| vDateMonday = vDateRef - iWeekday2 + 1 |
| End If |
| '<----- |
| |
| GetWeekMonday = vDateMonday |
| |
| |
| EXITSCRIPT: |
| Exit Function |
| |
| ERRORHANDLER: |
| Call ErrorMessage ("Function 'GetWeekMonday'") |
| Resume EXITSCRIPT |
| |
| |
| End Function |
Benutzt wird bei der Function noch folgende Function:
| Function CalculateIsoWeekday( tmpdate As Variant ) As Integer |
| |
| %REM |
| Function is used by GetCalendarWeek. |
| This function converts the weekday-numbers from the |
| standard function to an offset acc. to the ISO version |
| monday -> 0, ... , sunday -> 6 |
| %END REM |
| |
| On Error Goto ERRORHANDLER |
| |
| Dim n As Integer |
| |
| n = Weekday( tmpdate ) |
| |
| If n = 1 Then ' sunday to end of week |
| n = n + 7 |
| End If |
| |
| CalculateIsoWeekday = n - 2 |
| |
| EXITSCRIPT: |
| Exit Function |
| |
| ERRORHANDLER: |
| Call ErrorMessage ("Function 'GetDateRange'") |
| Resume EXITSCRIPT |
| |
| End Function |