Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 67
Default Date problem

Hi

Can anyone tell me please why the results of the following code:

Dim dte As Date
dte = Now()
ActiveCell.Value = Format(dte, "Short Date")
End Sub

Is 07/05/2007 (May 7, 2007) instead of 05/07/2007???

Thanks in advance

Eli
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Date problem

If you type a date into a cell, excel will parse it and display it according to
its rules. And your code is essentially just typing into that cell.

You want to control the format of the cell--not the format of what you're typing
(via code).

dim dte as date
dte = date
with activecell
.numberformat = "mm/dd/yyyy" 'or whatever you want
.value = dte
end with



??? wrote:

Hi

Can anyone tell me please why the results of the following code:

Dim dte As Date
dte = Now()
ActiveCell.Value = Format(dte, "Short Date")
End Sub

Is 07/05/2007 (May 7, 2007) instead of 05/07/2007???

Thanks in advance

Eli


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 67
Default Date problem

Thanks Dave!

"Dave Peterson" wrote:

If you type a date into a cell, excel will parse it and display it according to
its rules. And your code is essentially just typing into that cell.

You want to control the format of the cell--not the format of what you're typing
(via code).

dim dte as date
dte = date
with activecell
.numberformat = "mm/dd/yyyy" 'or whatever you want
.value = dte
end with



??? wrote:

Hi

Can anyone tell me please why the results of the following code:

Dim dte As Date
dte = Now()
ActiveCell.Value = Format(dte, "Short Date")
End Sub

Is 07/05/2007 (May 7, 2007) instead of 05/07/2007???

Thanks in advance

Eli


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Date problem

to add to Dave's explanation,

when you pass a date STRING from VB to Excel, it will be interpreted using
US date format (mm/dd/yyyy) if that would be a valid date.

As Dave said, pass it as a dateserial and it won't be misinterpreted.

--
Regards,
Tom Ogilvy


"Dave Peterson" wrote:

If you type a date into a cell, excel will parse it and display it according to
its rules. And your code is essentially just typing into that cell.

You want to control the format of the cell--not the format of what you're typing
(via code).

dim dte as date
dte = date
with activecell
.numberformat = "mm/dd/yyyy" 'or whatever you want
.value = dte
end with



??? wrote:

Hi

Can anyone tell me please why the results of the following code:

Dim dte As Date
dte = Now()
ActiveCell.Value = Format(dte, "Short Date")
End Sub

Is 07/05/2007 (May 7, 2007) instead of 05/07/2007???

Thanks in advance

Eli


--

Dave Peterson

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Date problem

Thanks for the addition.

I would have guessed that it picked it up from regional settings regional short
date format--but a minor test proved that you were right <vbg.

Tom Ogilvy wrote:

to add to Dave's explanation,

when you pass a date STRING from VB to Excel, it will be interpreted using
US date format (mm/dd/yyyy) if that would be a valid date.

As Dave said, pass it as a dateserial and it won't be misinterpreted.

--
Regards,
Tom Ogilvy

"Dave Peterson" wrote:

If you type a date into a cell, excel will parse it and display it according to
its rules. And your code is essentially just typing into that cell.

You want to control the format of the cell--not the format of what you're typing
(via code).

dim dte as date
dte = date
with activecell
.numberformat = "mm/dd/yyyy" 'or whatever you want
.value = dte
end with



??? wrote:

Hi

Can anyone tell me please why the results of the following code:

Dim dte As Date
dte = Now()
ActiveCell.Value = Format(dte, "Short Date")
End Sub

Is 07/05/2007 (May 7, 2007) instead of 05/07/2007???

Thanks in advance

Eli


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
date problem puiuluipui Excel Discussion (Misc queries) 11 November 4th 08 10:14 AM
Exel increment date problem wrt todays date. [email protected] Excel Worksheet Functions 1 November 11th 07 06:58 PM
Date Formula Problem - Leave date blank if Null Gayla Excel Worksheet Functions 5 April 24th 07 09:42 PM
Date problem Craig Johnson Excel Programming 3 April 4th 07 11:53 AM
Us date versus EU date problem Jan T. Excel Programming 7 October 2nd 06 07:06 PM


All times are GMT +1. The time now is 10:28 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright 2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"