Function StringToDate(strDate As String) As Variant
'Description: converts date string to date (long)
' applicable formats: "yyyy.(m)m.(d)d(.)", "yyyy/(m)m/(d)d(/)", "yyyymmdd"
'Inputs: date as string
'Outputs: date as long if conversion is successful, otherwise FALSE
'Dependency: CountCharacter; IsCorrectDate
'*** Deklarációk ***
Dim intYr As Integer
Dim intMo As Integer
Dim intDay As Integer
Dim arrDate As Variant
Dim S As Variant
Dim Separators As Collection
Const Separator1 As String = "."
Const Separator2 As String = "/"
'*******************
Set Separators = New Collection
With Separators
.Add Separator1
.Add Separator2
End With
' examine pre-defined separator characters
For Each S In Separators
If CountCharacter(strDate, Format(S, "@")) >= 2 And CountCharacter(strDate, Format(S, "@")) <= 3 Then
arrDate = Split(strDate, S)
intYr = arrDate(0)
intMo = arrDate(1)
intDay = arrDate(2)
If IsCorrectDate(intYr, intMo, intDay) = True Then
StringToDate = DateSerial(intYr, intMo, intDay)
Exit Function
Else
StringToDate = False
End If
End If
Next S
' if no separator was found check if date is valid without separator
If Len(strDate) = 8 Then
intYr = Left(strDate, 4)
intMo = Mid(strDate, 5, 2)
intDay = Right(strDate, 2)
If IsCorrectDate(intYr, intMo, intDay) = True Then
StringToDate = DateSerial(intYr, intMo, intDay)
Exit Function
End If
End If
StringToDate = False
End Function