Gruppe
UDF
Problem
Wie kann ich den ersten Tag einer beliebigen Kalenderwoche in einem beliebigen Jahr ermitteln.
StandardModule: Modul1
Private Function DINWeek(dat As Date) As Integer
Dim dValue As Double
dValue = DateSerial(Year(dat + (8 - WeekDay(dat)) Mod 7 - 3), 1, 1)
DINWeek = (dat - dValue - 3 + (WeekDay(dValue) + 1) Mod 7) \ 7 + 1
End Function
Function DINDay(iYear As Integer, iDIN As Integer)
Dim iDay As Integer, iWeek As Integer
If iYear = 0 Then
DINDay = 0
Exit Function
End If
iDay = 1
iWeek = DINWeek(DateSerial(iYear, 1, 1))
If iWeek <> 1 Then
Do Until DINWeek(DateSerial(iYear, 1, iDay)) = 1
iDay = iDay + 1
Loop
Else
Do Until DINWeek(DateSerial(iYear, 1, iDay)) <> 1
iDay = iDay - 1
Loop
iDay = iDay + 1
End If
DINDay = DateSerial(iYear, 1, iDay) + (iDIN - 1) * 7
End Function