Hallo Knud,
in meinen als "Prinzipstudie" aus dem Kopf hingetippten Code mag etwas nicht passen, ich kann das während des EntwicklerCamps in Gelsenkirchen auch nicht tiefer checken (kurze Nächte hier
).
Schau Dir mal mal folgenden definitiv funktionierenden Code unten an (da Dir sicherlich der "ErrorHandler" fehlt, musst Du da noch Hand anlegen. Wenn Du den Code nachvollziehen konntest, kannst Du ihn als "Blackbox" verwenden oder Deinen Gegebeneheiten anpassen.
HTH,
Bernhard
Function GetDateArray (vStartDate As Variant, vEndDate As Variant, szErrorMessage As String) As Variant
'===================================================================================================================================
' Purpose: Build from given start end and end date an array of date/time variants containing all days between start and end date
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' Arguments:
' vStartDate - First date of the period (included) - must be variant of type "Date/Time"
' vLastDate - Last date of the period (included) - must be variant of type "Date/Time"
' szErrorMessage - A variable to return possible error messages by reference
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' Returns: An array of variants containing all days between start and end date
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' Created by: Bernhard Koehler on 10.04.2004 Modified by: Bernhard Koehler on 14.10.2010
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' NOTES: We allow a maximum of 4000 elements in the resulting array to avoid an overflow
'------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' Changes: Enhanced error handling to avoid an overflow, new parameter szErrorMessage
'===================================================================================================================================
Dim vResult () As Variant
Dim iLoop As Integer
On Error Goto ErrorRoutine
'Check for correct values:
If (Datatype (vStartDate) <> V_DATE) Or (Datatype (vEndDate) <> V_DATE) Then
szErrorMessage = "Wrong data types used!"
Exit Function
End If
If vEndDate - vStartDate < 0 Then
szErrorMessage = "EndDate is before StartDate!"
Exit Function
End If
If vEndDate - vStartDate > 3999 Then
szErrorMessage = "You tried to build a datelist between " & Cstr (vStartDate) & " and " & Cstr (vEndDate) & " which would result in an overflow!"
Exit Function
End If
iLoop = vEndDate - vStartDate
Redim vResult (0 To iLoop)
vResult (0) = vStartDate
For iLoop = 1 To Ubound (vResult)
vResult (iLoop) = vStartDate + iLoop
Next
GetDateArray = vResult
Exit Function
ErrorRoutine:
Call ErrorHandler ("GetDateArray")
szErrorMessage = "Run-time error " & Error$ & " in line " & Cstr (Erl) & " (vStartDate: " & Cstr (vStartDate) & " vEndDate: " & Cstr (vEndDate)
Exit Function
End Function