Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 4
Default Date Format - simple question

Hi all,
Simple enough question but I can't find the answer. I have date values in a spreadsheet that can be in mmm-yy or dd-mmm-yy format e.g. sometimes Jan-11 other times 01-Jan-11. There is no problem when it's in dd-mmm-yy format, the problem is the ones that are just mmm-yy format.

When I try to convert the field to date format (format(myDate, "dd-mmm-yyyy")) it does the following:

Jan-11 CONVERTS TO 11-Jan-2012
Feb-10 CONVERTS TO 10-Feb-2012

So what I want to do is when a value is Jan-11, I want to convert it to 01-Jan-2011. If the value is 01-Jan-11 then convert it to 01-Jan-2011.

Thanking you in advance.
  #2   Report Post  
Junior Member
 
Posts: 4
Default

Dim r As Range
Dim t As Date

Set r = Selection.Cells(1) ' change as desired
If VarType(r.Value2) = vbDouble Then
t = r.Value
ElseIf IsDate(r.Text) Then
t = CDate("1 " & r.Text)
End If
MsgBox t

Quote:
Originally Posted by meljunk View Post
Hi all,
Simple enough question but I can't find the answer. I have date values in a spreadsheet that can be in mmm-yy or dd-mmm-yy format e.g. sometimes Jan-11 other times 01-Jan-11. There is no problem when it's in dd-mmm-yy format, the problem is the ones that are just mmm-yy format.

When I try to convert the field to date format (format(myDate, "dd-mmm-yyyy")) it does the following:

Jan-11 CONVERTS TO 11-Jan-2012
Feb-10 CONVERTS TO 10-Feb-2012

So what I want to do is when a value is Jan-11, I want to convert it to 01-Jan-2011. If the value is 01-Jan-11 then convert it to 01-Jan-2011.

Thanking you in advance.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 538
Default Date Format - simple question

meljunk wrote:

Simple enough question but I can't find the answer. I have date values
in a spreadsheet that can be in mmm-yy or dd-mmm-yy format e.g.
sometimes Jan-11 other times 01-Jan-11. There is no problem when it's in
dd-mmm-yy format, the problem is the ones that are just mmm-yy format.

When I try to convert the field to date format (format(myDate,
"dd-mmm-yyyy")) it does the following:

Jan-11 CONVERTS TO 11-Jan-2012
Feb-10 CONVERTS TO 10-Feb-2012

So what I want to do is when a value is Jan-11, I want to convert it to
01-Jan-2011. If the value is 01-Jan-11 then convert it to 01-Jan-2011.


When you enter a date with just two values, where they can be interpreted as
a day-month pair (e.g. Jan-11) Excel assumes you mean "January 11, [current
year]", not "January 2011". To avoid this in the future, you can enter the
date with the 4-digit year (since there's no 2011th day of January, Excel
picks the first day of that month).

My best suggestion would be to check the cell's .Text property, somewhat like
this:

t = ActiveCell.Text
y = InStr(InStr(ActiveCell.Text, "-") + 1, ActiveCell.Text, "-")
'if y then dd-mmm-yyyy else mmm-yy
If Not y Then t = "1-" & t
'format here using t

(But note that this code will get things wrong if it gets "11-Jan" instead of
"Jan-11": "11-Jan" - "1-11-Jan".)

--
Artificial by necessity.
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
looking for a simple question and answer format barcoda367 Excel Programming 1 July 18th 08 02:47 PM
Date question - really simple but I am stumped shhhhh Excel Programming 1 February 5th 07 09:20 AM
Simple Format Question Quin Excel Discussion (Misc queries) 2 December 17th 06 10:04 PM
Simple Question, display only part of a date in Number format DB Explorer Excel Worksheet Functions 6 March 17th 06 11:47 AM
Simple Format Question Ket Excel Worksheet Functions 1 March 2nd 05 04:10 PM


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