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

I nave a problem formatting a variable. If i run the following in a sub it
has no effect on the variable value, however if 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: 1
Default problem formatting a variable


Spike;525829 Wrote:
I nave a problem formatting a variable. If i run the following in a sub
it
has no effect on the variable value, however if 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


The *Format *function returns a string. You've specified the -datCFwd
-as a *Date *type variable and the date will always be held that way in
it (look at the -Locals pane- while stepping through the code).
If you pass the date to a sub with the likes of:
test datCFwd
and the *test *sub is:
Sub test(xxx)
Debug.Print xxx
End Sub
then the variable *xxx* becomes a -Variant/Date- - a bit more
flexible.
On the other hand if the receiving sub is:
Sub test(xxx As Date)
Debug.Print xxx
End Sub
then the *xxx *variable remains just a pure *Date *variable.

From the code's point of view, a date is just a date, the formatting
comes in when humans see it - so where, ultimately, is this formatted
date going to be seen by humans?
To solve your immediate problem, put the formatted date into a string
variable, or (the above seems to be a long way to get to this) just
leave off the *As Date* at the top of your existing code.


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=144305

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default problem formatting a variable

Is the variable declared in one sub and not in another? Is the variable
declared on the variable line oof the Sub?



"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, however if 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
formatting a variable problem Spike Excel Programming 3 October 16th 09 08:51 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 01:12 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"