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

I nave a problem formatting a variable. If i run the following in a sub it
has no effect on the variable value, howeverif i pass the variable to another
sub and run the same formatting code on it it does it as i want.

Dim datCFwd As Date

datCFwd = Sheets("Model").Range("Model_AccountingEnd").Value
datCFwd = Format(datCFwd, "dd mmm yy")

the range Sheets("Model").Range("Model_AccountingEnd") is custom formatted
as "dd-mmm-yy" but the variable actuallly holds the value as "dd/mm/yy" and
this does not change unless it is passed to another sub as explained above

Any ideas to correct this will be very gratefully received.

--
with kind regards

Spike
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default formatting a variable problem

First, using "As Date" will keep the value a date--not text (Format() returns
text).

So either use two variables or use "As Variant"

Second, when working with dates, it's probably better to use the .value2
property.

Spike wrote:

I nave a problem formatting a variable. If i run the following in a sub it
has no effect on the variable value, howeverif i pass the variable to another
sub and run the same formatting code on it it does it as i want.

Dim datCFwd As Date

datCFwd = Sheets("Model").Range("Model_AccountingEnd").Value
datCFwd = Format(datCFwd, "dd mmm yy")

the range Sheets("Model").Range("Model_AccountingEnd") is custom formatted
as "dd-mmm-yy" but the variable actuallly holds the value as "dd/mm/yy" and
this does not change unless it is passed to another sub as explained above

Any ideas to correct this will be very gratefully received.

--
with kind regards

Spike


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 220
Default formatting a variable problem

Dates are stored internally as single precision numbers, with the whole
number part representing the date and the decimal part representing the time
of day. Your Format statement does not change that - datCFwd will always
store the date as a real number. It will not store the date as a text
string, which is what the Format statement produces. So what you're doing is
taking the value that is stored in datCFwd, converting it to a text string
using Format, and then converting it back to a real number (using the "=").
You have changed nothing.

If you want a specific text string, you should have something like:

Dim datCFwdTxt as String

datCFwdTxt = Format(datCFwd, "dd mmm yy")

This will produce a text string with the desired format for the date.

HTH,

Eric

"Spike" wrote:

I nave a problem formatting a variable. If i run the following in a sub it
has no effect on the variable value, howeverif i pass the variable to another
sub and run the same formatting code on it it does it as i want.

Dim datCFwd As Date

datCFwd = Sheets("Model").Range("Model_AccountingEnd").Value
datCFwd = Format(datCFwd, "dd mmm yy")

the range Sheets("Model").Range("Model_AccountingEnd") is custom formatted
as "dd-mmm-yy" but the variable actuallly holds the value as "dd/mm/yy" and
this does not change unless it is passed to another sub as explained above

Any ideas to correct this will be very gratefully received.

--
with kind regards

Spike

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 140
Default formatting a variable problem

Spot on, many thanks for that much appreciated
--
with kind regards

Spike


"EricG" wrote:

Dates are stored internally as single precision numbers, with the whole
number part representing the date and the decimal part representing the time
of day. Your Format statement does not change that - datCFwd will always
store the date as a real number. It will not store the date as a text
string, which is what the Format statement produces. So what you're doing is
taking the value that is stored in datCFwd, converting it to a text string
using Format, and then converting it back to a real number (using the "=").
You have changed nothing.

If you want a specific text string, you should have something like:

Dim datCFwdTxt as String

datCFwdTxt = Format(datCFwd, "dd mmm yy")

This will produce a text string with the desired format for the date.

HTH,

Eric

"Spike" wrote:

I nave a problem formatting a variable. If i run the following in a sub it
has no effect on the variable value, howeverif i pass the variable to another
sub and run the same formatting code on it it does it as i want.

Dim datCFwd As Date

datCFwd = Sheets("Model").Range("Model_AccountingEnd").Value
datCFwd = Format(datCFwd, "dd mmm yy")

the range Sheets("Model").Range("Model_AccountingEnd") is custom formatted
as "dd-mmm-yy" but the variable actuallly holds the value as "dd/mm/yy" and
this does not change unless it is passed to another sub as explained above

Any ideas to correct this will be very gratefully received.

--
with kind regards

Spike

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
Conditional Formatting using variable text murkaboris Excel Discussion (Misc queries) 4 August 31st 09 03:07 AM
formatting a variable dstiefe Excel Programming 3 July 20th 06 07:25 PM
Set formatting variable Sige Excel Programming 4 September 29th 05 11:41 PM
Formatting a Variable Arturo Excel Programming 1 October 18th 04 10:38 PM
Problem trying to us a range variable as an array variable TBA[_2_] Excel Programming 4 September 27th 03 02:56 PM


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

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"