Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

[Excel] Valid Date Check

Status
Not open for further replies.

Skullmonkey

Computer
Joined
Dec 10, 2001
Messages
12
Location
US
I'm receiving a date in three parts (month,day,year) from a userform. Is there a prescripted VBA function that can check to see if this is a valid date? I have been using
Code:
dtDate = CDate(DateSerial(iYear,iDay,iMonth))
bValidDate = IsDate(dtDate)

to make this decision, but
Code:
DateSerial
will accept negative numbers, zeros, and numbers outside of my range (i.e. "70" as
Code:
iDay
is not a problem). Also, I'd like to make sure that the year is entered as a four-digit number.

Can I avoid hard-coding this? Or does Excel offer any tools for this sort of thing?

Any input would be appreciated.

SM
 
Don't see the perfect solution to your problem.
I propose two half way solutions (the second is preferred):
1)Transform your number to a string and use the date function that transforms it into a serial date: this function gives an error if day and month values are not valid, but accepts years in a two digits format.
2)Instead of checking the date with [tt]IsDate()[/tt], get back year, month and day from [tt]dtDate[/tt] with the corresponding functions and compare them with the input values: if they are the same then the input was OK. prex
motori@xcalcsREMOVE.com
Online tools for structural design
 
Maybe you could try something like this:
Code:
Dim sDate As String

sDate = Text1.Text & "/" & Text2.Text & "/" & Text3.Text
bValidDate = IsDate(sDate)
This removes the potential errors introduced by using the DateSerial method.

Hope this helps...

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top