Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Kdub via OfficeKB.com
 
Posts: n/a
Default Another Date Question

I have the following in a VBA add-in:

Rows(1).Insert
Range("A1").Value = "H"
Range("B1").Value = MemoNumber
Range("C1").Value = Date
Range("C1").NumberFormat = "m/d/yyyy"
Range("C1").NumberFormat = "@"

I use the last three lines to obtain a date in the format m/d/yyyy for
input to another process. Problem is, this doesn't always
work. I use the exact same code in various places in the
add-in. Sometimes it works, sometimes I don't get the format
I expect. I have just inserted the row - shouldn't this work
the same every time?

Kevin

--
Message posted via http://www.officekb.com
  #2   Report Post  
Dave Peterson
 
Posts: n/a
Default

First, I'd drop that last line:
Range("C1").NumberFormat = "@"

It can't be helping.

=========


You may have been hit by a coincidence.

When you do Format|cells|Number tab and look at that cell's format, I'm betting
you see something like:

*3/14/2001

That asterisk means that you chose a short date format that is picked up from
the Windows Regional settings (Date tab).

xl2002+ is more honest with the way it deals with dates.

At the bottom of that dialog (xl2002):

Date formats display date and time serial numbers as date values. Except for
items that have an asterisk (*), applied formats do not switch date orders with
the operating system.

Since yours is marked with an asterisk, your date will switch formats with the
setting in the OS.

=========

My windows short date format is mm/dd/yyyy. So I see 06/29/2005 (with the
leading 0s) when I run your code.

=========

Maybe a slight change to the format would work:
Range("C1").NumberFormat = "m/d/yyyy "
(an additional space)



"Kdub via OfficeKB.com" wrote:

I have the following in a VBA add-in:

Rows(1).Insert
Range("A1").Value = "H"
Range("B1").Value = MemoNumber
Range("C1").Value = Date
Range("C1").NumberFormat = "m/d/yyyy"
Range("C1").NumberFormat = "@"

I use the last three lines to obtain a date in the format m/d/yyyy for
input to another process. Problem is, this doesn't always
work. I use the exact same code in various places in the
add-in. Sometimes it works, sometimes I don't get the format
I expect. I have just inserted the row - shouldn't this work
the same every time?

Kevin

--
Message posted via http://www.officekb.com


--

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 function question John Day Excel Discussion (Misc queries) 3 March 9th 05 03:55 PM
Date Format Question Josh O. Excel Discussion (Misc queries) 1 February 10th 05 09:45 PM
SUMPRODUCT with date range question Rob V Excel Discussion (Misc queries) 1 January 31st 05 03:55 PM
Cell / Date Formatting Question when result = -0- seve Excel Discussion (Misc queries) 3 January 17th 05 02:29 AM
Template / date question macamarr Excel Worksheet Functions 1 November 17th 04 06:24 AM


All times are GMT +1. The time now is 12:41 AM.

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

About Us

"It's about Microsoft Excel"