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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Validation in VBA for Excel/Word

Status
Not open for further replies.

MyFlight

Technical User
Feb 4, 2002
193
0
0
HELP,

Does anyone know how I can Validate the Date using Excel/Word (VBA). What I am looking to do is if the Date is After 2/28/2004 (EXAMPLE), i want to cancel Script execution.

Also, If I Can break out the Date into Day, Month, and Year, I can then validate base on all or one of the Three.

Any and all suggestions will be greatly appreciated.
 
Hi MyFlight,

Not much to go on, but what about

if date > #2/28/2004# then ...

and have you tried ...

Day(date)
Month(date)
and Year(date)?


Enjoy,
Tony

------------------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading FAQ222-2244 before you ask a question.
 
To test whether a variable is a date, you can use:

If VarType(x) = vbDate Then

To convert a date that's in string form, you can use CDate:

x = "1/1/04"
y = CDate(x)

Once you know y is a valid date variable, you can make your comparison to your cutoff date. You can also then use the Month(y), Year(y), and Day(y) functions.

Let me know if this helps!

VBAjedi [swords]
bucky.gif
 
Hi:

My usual quick and dirty method to check the validity of the date field in cleaning up input files is to put the data in a text file (.txt) and then open it with Excel. Use tab-separated data fields. What Excel can't interpret as a date shows up very clearly.

-- Roy Zider

JAN. 12, 2005 did not recognize
12-Jan-05 recognized OK
MAR. 12. 2003 did not recognize
12-Mar-03 recognized OK
 
Hi again:

I checked the CDate() function a couple of days ago and it's better than Excel for extracting dates from various date strings. Use CDate() to get your input into Date type. Precede the call with an On Error Goto Err_Datetext statement pointing to your error code. CDate will throw a error number 13 'Type Mismatch' on input it can't convert.

-- Roy Zider
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top