VBA Date formatting inconsistencies

Shaft120

New Member
Joined
Sep 24, 2009
Messages
14
Hi I'm struggling with the way Excel treats dates and cannot find a straightforward logic that works 100%

Firstly

I was trying to import a sheet from a .csv file through VBA which contained Uk date fields:

When opening it through explorer it works fine, but through VBA it was treating the dates as if they were in american format (mm/dd/yyyy) and converting them into uk (which they already were, so it was actualy making them wrong), but for those dates that it couldn't convert (more than 12 days) it left them as general format even though local settings on my system are UK.

I fixed this through adding the "local:=True" qualifier when opening the worksheet I was copying from, forcing VBA to treat them as UK.

Secondly

However, I also have a piece if code further on, which goes through each cell to ensure that the date format has a 4 digit code for the year (see below). At this point VBA then again thinks that the dates are shown in american format and tries to convert them back to UK. Even though if you pause the code before this and manually check the formatting of the sheet, it shows as being correctly formatted to UK already.



Code:
Set WkbTemp = Workbooks.Open(Filename:=FilesToOpen, local:=True)
 
LSht = WkbAll.Sheets.Count
 
WkbTemp.Sheets(1).Copy After:=WkbAll.Sheets(LSht)
 
Set SSht = ActiveSheet
 
'Reformat Dates
 
For Dts = SFRow To SlRow
SSht.Range("E" & Dts).Value = Format(SSht.Range("E" & Dts).Value, "dd/mm/yyyy")
Next Dts


I can write something to manually seperate out the dates and do a long winded check and correction for 2 digit or 4 digit dates, but I want to understand why Excel is being inconsistent and if I am missing something that would be much easier. :confused:

All help greatly appreciated.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
VBA believes that the US is the only country in the world worth considering so it defaults to US dates unless you tell it otherwise. When writing dates to a sheet, you should use CDate (or DateValue) rather than Format, otherwise it interprets the value as a string representation of a date and converts it using US format if it can.
 
Upvote 0
VBA believes that the US is the only country in the world worth considering so it defaults to US dates unless you tell it otherwise. When writing dates to a sheet, you should use CDate (or DateValue) rather than Format, otherwise it interprets the value as a string representation of a date and converts it using US format if it can.


Thanks for getting back to me.

I still can't get this to work. I've tried uing CDate, but then I still need Excel to show the date in a 4 digit year, so I had to use format afterwards anyway. This would be fine, but it doesn't work as far as I can see with either DateFormat or CDate:

Code:
    For Dts = SFRow To SlRow
        SSht.Range("E" & Dts).Value = CDate(Format(SSht.Range("E" & Dts).Value, "dd/mm/yyyy"))
    Next Dts

or

Code:
    For Dts = SFRow To SlRow
        SSht.Range("E" & Dts).Value = DateFormat(Format(SSht.Range("E" & Dts).Value, "dd/mm/yyyy"))
    Next Dts

The data I'm trying to convert is a small list of dates:


<TABLE style="WIDTH: 46pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=61><COLGROUP><COL style="WIDTH: 46pt; mso-width-source: userset; mso-width-alt: 2230" width=61><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: #ccffff; WIDTH: 46pt; HEIGHT: 12.75pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: #ece9d8" class=xl82 height=17 width=61></TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: #ccffff; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl83 height=17>Dividend</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: #ccffff; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl84 height=17>Pay Date</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl85 height=17></TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl85 height=17></TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl85 height=17 align=right>18/12/09</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl85 height=17 align=right>04/06/10</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl85 height=17 align=right>17/09/10</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl85 height=17 align=right>15/09/10</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl85 height=17 align=right>14/05/10</TD></TR></TBODY></TABLE>

bit when I run the code in any way, it either stays the same (by adding CDate or DateFormat) or converts to this if I either leave it out or convert to dd/mm/yyyy after CDate / DateFormat:

<TABLE style="WIDTH: 57pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=76><COLGROUP><COL style="WIDTH: 57pt; mso-width-source: userset; mso-width-alt: 2779" width=76><TBODY><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: #ccffff; WIDTH: 57pt; HEIGHT: 12.75pt; BORDER-TOP: windowtext 0.5pt solid; BORDER-RIGHT: #ece9d8" class=xl84 height=17 width=76></TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: #ccffff; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl85 height=17>Dividend</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: #ccffff; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl86 height=17>Pay Date</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl87 height=17></TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl87 height=17></TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl87 height=17>18/12/2009</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl87 height=17 align=right>06/04/10</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl87 height=17>17/09/2010</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl87 height=17>15/09/2010</TD></TR><TR style="HEIGHT: 12.75pt" height=17><TD style="BORDER-BOTTOM: #ece9d8; BORDER-LEFT: #ece9d8; BACKGROUND-COLOR: transparent; HEIGHT: 12.75pt; BORDER-TOP: #ece9d8; BORDER-RIGHT: #ece9d8" class=xl87 height=17>14/05/2010</TD></TR></TBODY></TABLE>
 
Upvote 0
If the data is actually stored as a date (as it would appear since it's all right-aligned), then all you need to do is alter the formatting to show 4 digit year.
 
Upvote 0
If the data is actually stored as a date (as it would appear since it's all right-aligned), then all you need to do is alter the formatting to show 4 digit year.


I love it when somebody can point out my stupidity! :laugh:

I've clearly been so busy working around date formatting conversions, I didn't even see the wood for the trees.

Have to say though, Excel and Dates are a ***** nightmare.

Thanks for the help :bow:
 
Upvote 0
Hi Rory, I'm having problems with CDATE today. I've got a date
10/01/2011
and I'm using CDATE to see if it's after today. But it's converting the date to #01/10/2011# which is of course after today... it's fine if it's an impossible US date (say 25/01/2011) but if it can convert it to US format it will.

Dates are set to UK format on Control Panel.

Any suggestions?
 
Upvote 0
CDate should only convert to your regional format. What precisely are you passing to Cdate?
 
Upvote 0
Not entirely sure. It should be a string, it's from SAP so has '.' separators, I do a worksheetfunction.substitute setting the value (I know I can use replace now, just saw that in another post). But Excel seems happy to recognise it as a date on the worksheet.

All I want to do is see if it's after today. I can't just pick bits out of a string, as I don't know what format people will use - it's filled in by people all over the place who inisist on doing things differently.
 
Upvote 0
When I said 'precisely' I meant 'precisely'... ;)
 
Upvote 0
I'll have a play around. When I wrote a noddy bit of code (which does the same as the main code) it worked properly, but the main code doesn't and they do the same thing.
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top