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

Compare dates in VBA

Status
Not open for further replies.

Dazza1970

Programmer
Sep 28, 2005
3
GB
I'm trying to compare a date, that I grab from an excel cell, to see if it falls between two dates, as in:

(Not real code)

variable = excel cell value

Select case variable
case ((variable >= firstdate) AND (variable<=seconddate)
.....

The problem i'm getting is that it is treating the variables as strings, even if I use a dim statement to make sure it is a Date.
Any help would be appreciated.

I thought it would be as simple as on the excel sheet i.e.
Date1 > Date2 etc
 


Hi,

Why do Dates and Times seem to be so much trouble? faq68-5827


Skip,

[glasses] [red]Be Advised![/red]
A wee deranged psychic may be runnin' around out there!
SMALL MEDUIM @ LARGE[tongue]
 

...chances are that you do NOT really have DATES.

Rather you have STRINGS that you will need to convert to DATES, using the DateSerial function or DateValue, for instance.


Skip,

[glasses] [red]Be Advised![/red]
A wee deranged psychic may be runnin' around out there!
SMALL MEDUIM @ LARGE[tongue]
 
I thought I was converting the cell I grabbed from excel into a date. Still not working, no matter what I do.
Failing that is there any method or function in VBA to convert a date into its equivalent serial number?
 
This one:

Select case variable
case ((variable >= firstdate) AND (variable<=seconddate)

does not make any sence. It should be at least like that:

Select case true
case ((variable >= firstdate) AND (variable<=seconddate)
 


as already stated...

DateSerial function or DateValue function.


Skip,

[glasses] [red]Be Advised![/red]
A wee deranged psychic may be runnin' around out there!
SMALL MEDUIM @ LARGE[tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top