is date?

A

Arne Hegefors

Hi! i want to check if dates that i use as input into a program are valid
dates ie they exist. Now i have a rudimenatry control using the isDate
function in vba. however that does not do it if i write an invalid date
usijng the correct date format eg 2007-04-33. is there any way to check to
see if a date is ok or not? i would really appreciate help regarding this
matter. thanks alot!
 
G

Gary''s Student

Sub arne()
Dim s As String
Dim d As Date
s = Application.InputBox("enter date", 2)
MsgBox (s)
d = DateValue(s)
MsgBox (d)
End Sub


gets the date as a pure string, can accept many formats. Accepts 2007-4-21
will raise an error on 2007-4-33.

Just add on error code to handle it the way you want.
 
N

NickHK

Arne,
In the immediate window:
?isdate("2007-04-33")
False

You mean this returns true for you ?

NickHK
 
A

Arne Hegefors

Hi Nick! Yes to me it seems like isDate only gives false when the actual
format is no good. when the date is nonsens but format is godd eg 2007-04-35
vba reads it like 00:00:00. si it fixed it lik this:

If IsDate(datStartdate) = True Then
If datStartdate = "00:00:00" Then
MsgBox "Invalid start date. Please enter valid start date.",
vbOKOnly, "Error message"
Exit Sub
End If
End If





"NickHK" skrev:
 
D

Dave Peterson

Maybe you can check the year to see if it's valid for your data:

Dim datStartDate As Date
datStartDate = Application.InputBox(Prompt:="Enter a date", Type:=1)
If Year(datStartDate) < 2005 _
Or Year(datStartDate) > 2010 Then
MsgBox "Please enter a valid date"
Else
MsgBox Format(datStartDate, "mmmm dd, yyyy")
End If

Application.inputbox with type:=1 won't let the user enter 2007-04-35.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top