View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Jacob Skaria Jacob Skaria is offline
external usenet poster
 
Posts: 8,520
Default Converting a String into a date format

The below function returns a boolean. Arguments to be passed date string and
format string

If IsValidDate("23-12-1997", "dd-mm-yyyy") = False Then
MsgBox "Invalid Date"
End If

Function IsValidDate(strDate, strFmt)
Dim intx, strDD, strMM, strYY
If Len(strDate) < Len(strFmt) Then IsValidDate = False: Exit Function
For intx = 1 To Len(strDate)
Select Case (Mid(UCase(strFmt), intx, 1))
Case "D"
strDD = strDD & Mid(UCase(strDate), intx, 1)
Case "M"
strMM = strMM & Mid(UCase(strDate), intx, 1)
Case "Y"
strYY = strYY & Mid(UCase(strDate), intx, 1)
End Select
Next
If CInt("0" & strMM) < 1 Or CInt("0" & strMM) 12 Then IsValidDate = False:
Exit Function
If strDD = "" Then strDD = "1"
If strYY = "" Then strYY = Year(Date)
IsValidDate = IsDate(strDD & " " & MonthName(CInt(strMM)) & ", " & strYY)
End Function

--
If this post helps click Yes
---------------
Jacob Skaria


"Sardonic" wrote:

Dear All,

I am writing in VBA for Excel 2003 a function to validate Date values which
are inputted via a set of combo boxes, one each for day, month and date. I
am currently puzzling how to go about convering the string "DD\MM\YYYY" into
a true date value to check if it is a real date.

The code is given below - I'm sure I'm missing something simple....

Thanks for any assistance.

Regards

Function Parse_Date(Day As String, Month As String, Year As String, Source
As String) As Boolean
Dim FullDate As String

FullDate = Day & "/" & Month & "/" & Year

Dim DateFormatted As Date

DateFormatted = Format(FullDate, "longdate")

Do While (IsDate(DateFormatted) < True)
MsgBox (FullDate & " entered in " & Source & " is not a real date.")
Loop

Parse_Date = True

End Function