View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Ron Rosenfeld Ron Rosenfeld is offline
external usenet poster
 
Posts: 5,651
Default Check if date is valid

On Wed, 19 Oct 2005 03:23:48 -0500, Lava
wrote:


I've got an Excel sheet for data-input. For dates it should be possible
to define either a year or a complete date (day-month-year). If one
defines a year it is being converted to 01-01-year. In latter case the
exact date is not known or doesn't matter.

Anyway, for this reason I cannot use the DateFormat for the cells of a
column ("E"), because it would turn 2005 into something like 12-03-1905
or something similar I can't use. I've formatted the cells as Text.

How to check if the entered full date is a valid date?

I can check with CInt(Mid(Cells(i, "E"), 4, 2)) 12 in a For-loop with
parameter 'i' per row and similar If-constructions if the individual day
and month field are not faulty, but 31 April or 29 February (when
there's only 28 days) shouldn't pass the check.

So I'm wondering how I can check the dates? IF it's possible.

Later on the Excel sheet's data will be imported by a PHP script where
all fields are checked again just to be on the safe side and properly
inserted into the database. Question "why bother with Excel-level
checking?". Answer: it's been requested by my employer. If isolated day
/ month checking is as far as I can go... okay, nothing to be done about
that, but IF there's a way to check if a complete date is valid....
please share your thoughts :)


Why not leave column E formatted as General.

If someone enters just a year, that's what they'll see.

If the enter a date, then Excel should parse into a date.

When you check it in your VBA routine, the real dates will return TRUE with
ISDATE(value). The years will return FALSE and can then be tested to see if
they are an integer between your acceptable year range for date.


--ron