Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default What's wrong with this picture? VBA Date code..

I am attempting to format a date within Excel as DD-MMM-YYYY where the
MMM would be all caps. The only way that I have seen to do this so far
is to write a VB snippet that would convert it to text. MS actually has
sample code in KB Q213503 which works fine, it's listed below:

Sub UpperMonth()
Dim Cell As Object 'Declare the Cell variable.
For Each Cell In Selection
' If the cell is blank or a text string, then
' skip to the next cell in the selection.
If Cell.Value < "" And Val(Cell.Value) 0 Then
'Format the cell as text in a 'mmm' number format,
'and change it to uppercase.
Cell.Value = UCase(Format(Cell.Value, "mmm dd yyyy"))
End If
Next
End Sub

The only catch is if I try and change the date mask to "dd-mmm-yyyy",
the code does nothing. If I throw in a variable in the code and step
thru it I can see it will convert the date to MyStr as say 15 0CT 2003
as a string, but never changes the cell value..i.e.

Sub UpperMonth()
Dim MyStr
Dim Cell As Object 'Declare the Cell variable.
For Each Cell In Selection
' If the cell is blank or a text string, then
' skip to the next cell in the selection.
If Cell.Value < "" And Val(Cell.Value) 0 Then
'Format the cell as text in a 'mmm' number format,
'and change it to uppercase.
MyStr = UCase(Format(Cell.Value, "dd mmm yyyy"))
Cell.Value = UCase(Format(Cell.Value, "dd mmm yyyy"))
End If
Next
End Sub

Any ideas?
Thanks,
Mike



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,253
Default What's wrong with this picture? VBA Date code..


Format the cell as "@" (Text) prior to inserting the datestring
Note that the cell will now be stored as TEXT, so you cannot
use it in calculations.


Sub UpperMonth()

Dim Cell As Object 'Declare the Cell variable.
For Each Cell In Selection
' If the cell is blank or a text string, then
' skip to the next cell in the selection.
If Cell.Value < "" And Val(Cell.Value) 0 Then
'Format the cell as text in a 'mmm' number format,
'and change it to uppercase.
Cell.NumberFormat = "@"
Cell.Value = UCase(Format(Cell.Value, "dd mmm yyyy"))
End If
Next
End Sub


keepITcool

< email : keepitcool chello nl (with @ and .)
< homepage: http://members.chello.nl/keepitcool


mriley wrote:

I am attempting to format a date within Excel as DD-MMM-YYYY where the
MMM would be all caps. The only way that I have seen to do this so far
is to write a VB snippet that would convert it to text. MS actually

has
sample code in KB Q213503 which works fine, it's listed below:

Sub UpperMonth()
Dim Cell As Object 'Declare the Cell variable.
For Each Cell In Selection
' If the cell is blank or a text string, then
' skip to the next cell in the selection.
If Cell.Value < "" And Val(Cell.Value) 0 Then
'Format the cell as text in a 'mmm' number format,
'and change it to uppercase.
Cell.Value = UCase(Format(Cell.Value, "mmm dd yyyy"))
End If
Next
End Sub

The only catch is if I try and change the date mask to "dd-mmm-yyyy",
the code does nothing. If I throw in a variable in the code and step
thru it I can see it will convert the date to MyStr as say 15 0CT 2003
as a string, but never changes the cell value..i.e.

Sub UpperMonth()
Dim MyStr
Dim Cell As Object 'Declare the Cell variable.
For Each Cell In Selection
' If the cell is blank or a text string, then
' skip to the next cell in the selection.
If Cell.Value < "" And Val(Cell.Value) 0 Then
'Format the cell as text in a 'mmm' number format,
'and change it to uppercase.
MyStr = UCase(Format(Cell.Value, "dd mmm yyyy"))
Cell.Value = UCase(Format(Cell.Value, "dd mmm yyyy"))
End If
Next
End Sub

Any ideas?
Thanks,
Mike



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from

http://www.ExcelForum.com/



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default What's wrong with this picture? VBA Date code..

Worked like a champ...thanks!



------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~ View and post usenet messages directly from http://www.ExcelForum.com/

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
What is wrong with this code? Ayo Excel Discussion (Misc queries) 14 June 10th 08 03:09 AM
What is wrong with this code? jlclyde Excel Discussion (Misc queries) 5 January 9th 08 05:12 PM
Can someone tell me what is wrong with this code? Ant Excel Discussion (Misc queries) 8 November 14th 05 02:53 PM
What's wrong with my code ? christophe meresse Excel Worksheet Functions 3 August 2nd 05 05:09 PM
Is something wrong with the code Patrick Molloy[_3_] Excel Programming 1 July 15th 03 08:28 AM


All times are GMT +1. The time now is 10:24 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"