View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Converting a String into a date format

First off, the names of your arguments (Day, Month, Year) are not the best
as these are the names of built-in VB functions. Second, the loop you are
running is not a real loop and can probably be replaced with a simple
If..Then block. Also, what is the Source argument doing? Anyway, it is not
clear to me what your function is supposed to be doing. My guess, though, is
you want know if the inputted day, month and year values produce a valid
date or not. If that is what you are doing, then consider something like
this...

Function Parse_Date(D As String, M As String, Y As String, _
Source As String) As Boolean
Dim DateIn As Date
DateIn = DateSerial(Y, M, D)
Parse_Date = Year(DateIn) = Y And Month(DateIn) = M And Day(DateIn) = D
If Not Parse_Date Then
MsgBox D & "/" & M & "/" & Y & " entered in " & _
Source & " is not a real date."
End If
End Function

--
Rick (MVP - Excel)


"Sardonic" wrote in message
...
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