PC Review


Reply
Thread Tools Rate Thread

DateValue gets wrong date number

 
 
StuartBisset
Guest
Posts: n/a
 
      14th May 2009
Hi Folks

I have written a macro that grabs data from a seperate CSV file and
puts it into my workbook. The CSV file is a download from another
system and there was an issue with some dates coming out in numeric
format and some coming out in text format. I got round this by
using ...

DateValue(wkbCSV.cells(xLoop,1))*1 (where "wbkCSV" is the CSV file
and "xLoop" cycles thru all the rows)

.... to put the date integers into my array.

However the CSV file shows 01/04/2009 (ie 1st April, integer 39904)
but when I spit the array out onto the worksheet it shows as
04/01/2009 (ie 4th Jan, integer 39817).

There is not a problem with my regional settings in control panel (set
to dd/mm/yyyy) or with the number format in excel (also set to dd/mm/
yyyy).

When I select the date cell in the csv file and I go to the immediate
window in the VBE and type ....

? DateValue(selection)*1

.... it returns 39904 (correct).

But when I run my macro it seems to grab 39817 instead. How can this
be?

Any help would be much appreciated.

Cheers

Stuart
 
Reply With Quote
 
 
 
 
Patrick Molloy
Guest
Posts: n/a
 
      14th May 2009
what is actually in the CSV file? open it in Notepad as Excel cleverly tries
to interpret the data


"StuartBisset" <(E-Mail Removed)> wrote in message
news:f32503eb-6eb6-46b4-9792-(E-Mail Removed)...
> Hi Folks
>
> I have written a macro that grabs data from a seperate CSV file and
> puts it into my workbook. The CSV file is a download from another
> system and there was an issue with some dates coming out in numeric
> format and some coming out in text format. I got round this by
> using ...
>
> DateValue(wkbCSV.cells(xLoop,1))*1 (where "wbkCSV" is the CSV file
> and "xLoop" cycles thru all the rows)
>
> ... to put the date integers into my array.
>
> However the CSV file shows 01/04/2009 (ie 1st April, integer 39904)
> but when I spit the array out onto the worksheet it shows as
> 04/01/2009 (ie 4th Jan, integer 39817).
>
> There is not a problem with my regional settings in control panel (set
> to dd/mm/yyyy) or with the number format in excel (also set to dd/mm/
> yyyy).
>
> When I select the date cell in the csv file and I go to the immediate
> window in the VBE and type ....
>
> ? DateValue(selection)*1
>
> ... it returns 39904 (correct).
>
> But when I run my macro it seems to grab 39817 instead. How can this
> be?
>
> Any help would be much appreciated.
>
> Cheers
>
> Stuart


 
Reply With Quote
 
StuartBisset
Guest
Posts: n/a
 
      14th May 2009

What is in notepad is 01/04/2009

(ie the correct date written in a date format)
 
Reply With Quote
 
Bernie Deitrick
Guest
Posts: n/a
 
      14th May 2009
Stuart,

VBA dates use US default - mm/dd/yyyy, not dd/mm/yyyy - as the first interpretation of the date
string. If that fails (the dd value is 13 or higher), then it tries dd/mm/yyyy.

So you need to extract your month and day:

Dim myVals As Variant
Dim myDay As Integer
Dim myMonth As Integer
Dim myYear As Integer

myVals = Split(wkbCSV.Cells(xLoop, 1), "/")
myDay = myVals(0)
myMonth = myVals(1)
myYear = myVals(2)

myArr(xLoop) = DateSerial(myYear, myMonth, myDay)

Should work... not sure what your array is named...

HTH,
Bernie
MS Excel MVP


"StuartBisset" <(E-Mail Removed)> wrote in message
news:f32503eb-6eb6-46b4-9792-(E-Mail Removed)...
> Hi Folks
>
> I have written a macro that grabs data from a seperate CSV file and
> puts it into my workbook. The CSV file is a download from another
> system and there was an issue with some dates coming out in numeric
> format and some coming out in text format. I got round this by
> using ...
>
> DateValue(wkbCSV.cells(xLoop,1))*1 (where "wbkCSV" is the CSV file
> and "xLoop" cycles thru all the rows)
>
> ... to put the date integers into my array.
>
> However the CSV file shows 01/04/2009 (ie 1st April, integer 39904)
> but when I spit the array out onto the worksheet it shows as
> 04/01/2009 (ie 4th Jan, integer 39817).
>
> There is not a problem with my regional settings in control panel (set
> to dd/mm/yyyy) or with the number format in excel (also set to dd/mm/
> yyyy).
>
> When I select the date cell in the csv file and I go to the immediate
> window in the VBE and type ....
>
> ? DateValue(selection)*1
>
> ... it returns 39904 (correct).
>
> But when I run my macro it seems to grab 39817 instead. How can this
> be?
>
> Any help would be much appreciated.
>
> Cheers
>
> Stuart



 
Reply With Quote
 
Patrick Molloy
Guest
Posts: n/a
 
      14th May 2009
As Bernie indicates, this would be interpreted by Excel as a US date, ie Jan
4th not 1st Apr

another way around this would be to read the data into excel using VBA and
then manipulating the text appropriately.

"StuartBisset" <(E-Mail Removed)> wrote in message
news:36d2912a-db4b-4259-871a-(E-Mail Removed)...
>
> What is in notepad is 01/04/2009
>
> (ie the correct date written in a date format)


 
Reply With Quote
 
Ron Rosenfeld
Guest
Posts: n/a
 
      14th May 2009
On Thu, 14 May 2009 10:27:30 -0400, "Bernie Deitrick" <deitbe @ consumer dot
org> wrote:

>Stuart,
>
>VBA dates use US default - mm/dd/yyyy, not dd/mm/yyyy - as the first interpretation of the date
>string. If that fails (the dd value is 13 or higher), then it tries dd/mm/yyyy.
>


Bernie,

I've seen this written a bunch of times and never checked it out. But today I
did and, at least on my system (VBA 6.5), Datevalue behaves exactly as
documented.

According to HELP, "DateValue recognizes the order for month, day, and year
according to the Short Date format you specified for your system. "

So I did the following in the Immediate window:

?format(datevalue("01/04/2009"),"dd-mmm-yyyy")

With my default US settings, the result was:

04-Jan-2009

However, when I changed the Short Date format in my regional settings to
d/M/yyyy, the result was:

01-Apr-2009

I then changed it again to UK settings, and again got the

01-Apr-2009

result.

So it seems to me there is something else going on than VBA date handling
issues.
--ron
 
Reply With Quote
 
StuartBisset
Guest
Posts: n/a
 
      14th May 2009
Bernie, Patrick

Many thanks for your help. Your "Split" solution looks like it will
work and for that I am grateful - but I must admit I find it extremely
odd that DateValue would return the correct value when it was used in
the immediate window but a different/incorrect value when used within
a macro. Can either of you explain the reason for that?

Cheers

Stuart
 
Reply With Quote
 
Patrick Molloy
Guest
Posts: n/a
 
      14th May 2009
MS is an American company?



no offence guys!!!

"StuartBisset" <(E-Mail Removed)> wrote in message
news:fb2169af-da5b-4968-bab8-(E-Mail Removed)...
> Bernie, Patrick
>
> Many thanks for your help. Your "Split" solution looks like it will
> work and for that I am grateful - but I must admit I find it extremely
> odd that DateValue would return the correct value when it was used in
> the immediate window but a different/incorrect value when used within
> a macro. Can either of you explain the reason for that?
>
> Cheers
>
> Stuart


 
Reply With Quote
 
StuartBisset
Guest
Posts: n/a
 
      14th May 2009
Ron

That is interesting. I have just run the suggested SPLIT solution and
I'm afraid it didn't work (sorry Bernie!).

The macro is somehow swapping round the myDay & myMonth values - i
have checked all this using debug.print as well as looking at the end
results in the worksheet.

Does anyone have any other smart ideas????

Cheers folks

Stuart
 
Reply With Quote
 
Bernie Deitrick
Guest
Posts: n/a
 
      14th May 2009
Stuart,

You should only run the Split on cells that are text and not values - otherwise, the value is a
date - a number - without \s.

HTH,
Bernie
MS Excel MVP


"StuartBisset" <(E-Mail Removed)> wrote in message
news:8edbd7bf-de9d-4968-a175-(E-Mail Removed)...
> Ron
>
> That is interesting. I have just run the suggested SPLIT solution and
> I'm afraid it didn't work (sorry Bernie!).
>
> The macro is somehow swapping round the myDay & myMonth values - i
> have checked all this using debug.print as well as looking at the end
> results in the worksheet.
>
> Does anyone have any other smart ideas????
>
> Cheers folks
>
> Stuart



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date-related problems - max and datevalue Dimitri Ulyinov Microsoft Excel Misc 3 28th May 2006 05:34 PM
Date Search Not Working - Using DateValue() =?Utf-8?B?UGFwZXJiYWNrIFdyaXRlcg==?= Microsoft Access 5 27th Apr 2006 04:19 AM
DATEVALUE OF CURRENT DATE =?Utf-8?B?Sk9OQk9ZTUZMWQ==?= Microsoft Excel Worksheet Functions 5 29th Dec 2004 05:55 PM
what went wrong ?? - DateValue Query Mann Microsoft Access Queries 5 22nd Jun 2004 02:36 PM
What's wrong with my DateValue Query mann Microsoft Access Queries 1 22nd Jun 2004 11:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:00 AM.