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

Entering and Comparing dates in Excel VBA

Status
Not open for further replies.

phudgens

Technical User
Jul 8, 2004
117
US
I am having alot of difficulty getting VBA to recognize a date that I am calling StartDate (dim'ed as Date) for comparison with dates encountered in my input data. I've spent about an hour now trying different methods of inputting the date ("09/01/2009", "September 1, 2009", "09/01/2009", "09-01-2009", etc) along with various functions including DATE, DATEVALUE, etc. VBA invariably thinks my input date is 07/08/1894. How do I specify a date that VBA recognizes as date for comparison with other dates?

Thanks,
Paul Hudgens
 


Hi,

faq68-5827.

None of these are dates! They are STRINGS!!!
[tt]("09/01/2009", "September 1, 2009", "09/01/2009", "09-01-2009"[/tt]

DATE VALUES are NUMBERS like right now in North Texas it is 40045.66067.

You must CONVERT you String to DATE VALUES in VBA...
Code:
Dim d as date
d = #09/01/2009#
The # delimiters do the conversion.

You can also use the DateSerial function or the DataValue function. I prefer the former, as there can be no ambiguity.



Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Check out the online help for Datevalue. The example shows double quotes being used to specify a date, as is shown elsewhere in the online help. Nowhere can I find any reference to pound signs to specify a date. This does work though so thanks.

Paul Hudgens
 


Quotes in DateValue, YES, of course! One of the functions that I recommended to you. DateValue converts a STRING (delimited by quotes) to a Date Value.

Nowhere can I find any reference to pound signs to specify a date.
Check out Date Data Type in VBA Help.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top