4

It seems that every week or so someone posts a question about dates being converted (corrupted?) to American format. Like many others, I have attempted to help but the problem is elusive. I now wonder if I have discovered the cause.

I am working on an application in which I need to extract data from an Excel worksheet and output it as strings formatted to match the value the Excel user can see. So if the value is “1” formatted to display as “1.00” then I want the string to be “1.00”.

I achieve this effect by testing the cell value to be a number, date or time. If it is, I retrieve the number format and use it to format the cell value so:

With .Cells(Row, Column) 
  Output string = Format(.Value, .NumberFormat)
End With

In most cases this gives me exactly the output I require. However, sometimes I get American dates and times when the source is formatted as a UK date or time.

After much experimentation with Excel 2003 and Excel 2007, I have discovered the cause. (I do not have access to Excel 2010 but from questions I deduce it has the same problem.) This question is in part intended to reveal this problem to the world because I can discover nothing on the internet to indicate that anyone else has noticed it. (No doubt someone will reply that they googled “xyz” and got the answer immediately.) However, the main purpose of this question is to seek suggestions for obtaining the result I need in all situations.

Typically I enter dates as, for example, “23mar12”. Excel recognises this as a date and formats it as “23-Mar-12”. I can select Format Cells and enter or select a custom format or select one of the date formats so I can have any format I can imagine wanting including non-English names for days and months.

However, in one case the format I select is not the format that is recorded: Custom format “dd/mm/yyyy” is recorded as Date format “* 14/03/2001”. This is not obviously a problem until further down the line.

I created a column of dates and times and formatted each with a different custom or standard format. I wrote a macro to extract the NumberFormat for each of these dates and times and write it as a string to an adjacent column. I also formatted the value using the number format and wrote that string to a third column.

In a number of cases the format selected and recorded by Excel was not the format returned by NumberFormat:

Excel format            NumberFormat
Date: * 14/03/2001       m/d/yyyy
Date: * 14 March 2001   [$-F800]dddd, mmmm dd, yyyy
Date: 14/03/2001        dd/mm/yyyy;@
Date: 14/03/01          dd/mm/yy;@
Date: 14/3/01           d/m/yy;@
Date: 14.3.01           d.m.yy;@
Date: 2001-03-14        yyyy-mm-dd;@
Date: 14 March 2001 (1) [$-809]dd mmmm yyyy;@
Date: 14 March 2001 (2) [$-809]d mmmm yyyy;@
Custom: hh:mm:ss        h:mm:ss
Time: * 13:30:55        [$-F400]h:mm:ss AM/PM
Time: 13:30:55 (1)      hh:mm:ss;@
Time: 13:30:55 (2)      h:mm:ss;@
Time: 01:30:55 PM       [$-409]hh:mm:ss AM/PM;@
Time: 1:30:55 PM        [$-409]h:mm:ss AM/PM;@

The values (1) and (2) in the Excel format column were added by me to indicate that there are two apparently identical formats. As can be seen from the NumberFormat column, in each case the second version suppresses a leading zero.

Most changes have no important effect. “[$-F800]” and so on are apparently dummy values with no effect. Apparently you can replace “F800” with an Microsoft country code to have the names of days and months translated to the language of that country.

However, the three standard formats that Microsoft marks with an asterisk are changed unacceptably. The dates are changed from little endian to middle endian; the time is changed from 24 hour to 12 hour and the day of the week has been added to “* 14 March 2001”.

The asterisk against the dates, references the comment: “Except for items that have an asterisk () in the Type list (Number tab, Format Cells dialog box), date formats that you apply do not switch date orders with the operating system.” The asterisk against the time, references the comment: “Except for items that have an asterisk () in the Type list (Number tab, Format Cells dialog box), time formats that you apply do not switch time orders with the operating system.”

If I have to, I can warn my users that standard date and time formats may not give the result desired. However, if they want the popular format “dd/mm/yyyy”, they cannot have it. “dd-mm-yyyy”, for example, is OK but custom format “dd/mm/yyyy” becomes date format “* 14/03/2001” becomes “m/d/yyyy”.

Returning to my opening point: is this strange handling of one particular date format the reason so many people claim their dates are sometimes being converted to American format and is this why the problem is so elusive? I have come across this type of problem elsewhere of one group of Microsoft programmers not knowing what another group are doing. Is this why some functions always work and other sometimes don’t? Some Microsoft programmers know where to look for the correct format and others don’t?

More importantly, for me, can anyone suggest:

  • How I obtain the true date or time format?
  • Some other way of determining the user’s chosen display format for a date or time?

BTW 1: I recall that thirty or so years ago I was told that the American military do not use month/day/year format; only American civilians use this format. Can anyone tell me if this is true?

BTW 2: The similar problem is with Excel colours. Excel holds its colours as "ggbbrr" while everybody else holds them as "rrggbb". The programmers for the .Net Excel inter-op were not told and and did not reverse the Excel colour number before using it to control the screen.

Community
  • 1
  • 1
Tony Dallimore
  • 12,335
  • 7
  • 32
  • 61
  • Regarding your (1) and (2) bits, `d` shows a single digit or double digit day where as `dd` prepends a zero to single digit days. Similarly `h` gives single digit hours for less than 10 and `hh` prepends a zero. So the formats aren't the same, they merely return the same result if the respective day or hour is double digit. – Pete Dec 17 '14 at 22:29
  • Above I say: "The values (1) and (2) in the Excel format column were added by me to indicate that there are two apparently identical formats. As can be seen from the NumberFormat column, in each case the second version suppresses a leading zero." How does that differ from your comment? – Tony Dallimore Dec 17 '14 at 22:33
  • @Pete I forgot to include your name in the above comment. – Tony Dallimore Dec 18 '14 at 09:12
  • My bad. I missed the last few words there. – Pete Dec 18 '14 at 16:40
  • @Pete No problem. It was a long question with lots of points so it is easy to miss a single sentence. – Tony Dallimore Dec 18 '14 at 16:54

4 Answers4

2

I have mainly come up against formatting and date issues when opening text files which have been saved with different regional settings. Two useful cell properties for dealing with this are:

  • .Text returns the cell value as it is displayed
  • .Value2 returns the unformatted cell value or date serial number.

As you say, standard date and number formats depend on windows regional settings and this may not be desired behavior as the same workbook can display differently in different regions. MS introduced the regional code prefixes in number formats (circa Excel 2000?) which enforce consistent display if needed but they need to be explicitly selected.

If you really want to see a date or number as the user entered it, you could extract the contents of the .xlsx file looking at the worksheet cell format and the shared strings xml definitions which list the number formats in the saved workbook. I don't really see a need to do this though as the underlying value is stored internally as a serial number and this will not change.

lori_m
  • 5,487
  • 1
  • 18
  • 29
  • I was aware of the Text property but not of its significance. It produces the correct displayed value for every example I have constructed. Thank you. Please note, however, I am not importing text files nor do I seek the value the user entered. For Excel to take account of regional settings is good. To Excel to ignore regional setting and the user's requirement is wrong which is what the .NumberFormat property is doing. I still suspect this is the cause of many reported problems. However, you have solved my problem for which thank you again. – Tony Dallimore Mar 23 '12 at 17:37
  • I'm glad the .Text property fixed your issue. I see more clearly your point about `.NumberFormat` now - it does not respond to regional settings in date formats which does indeed look like a bug to me. I have come across something similar with `Evaluate` which always treats dates in US format and has caused me some grief in the past. – lori_m Mar 24 '12 at 09:38
0

BTW 1: It's been almost 30 years since I was in the military... I worked on helicopters and I was taught to use a format such as this in the aircraft logbooks: 3 Apr 12. So, that's how I still write dates. This way, there's no wondering about 4/3/2012 - is it April 3 or March 4?

Kevin
  • 1
0

I hacked this: I rewrite the original data in a known format. it relies on DateSerial and TimeSerial:

'Google spreadsheet stores dates in USA format (MM/DD/YYYY).  We're in Australia, using DD/MM/YYYY, so we need to swap them.
                    '
                        With dc 'the cell who contains a date in USA format.
                             d = .Value      'capture value in USA format
                             t = TimeValue(d)
                             .NumberFormat = "dd/mm/yyyy" 'set to OZ format, so Excel knows the values were swapped in its internal math.
                             .Value = DateSerial(Year(d), Month(d), Day(d)) 'DateSerial takes y,d,m. We swap Month and Day components, to get OZ format dates
                             .Value = .Value + TimeSerial(Hour(t), Minute(t), Second(t))
                             dc.Font.Bold = True                         ' We bold the cells that are swapped, for debugging
                         End With
                    End If
joshoff
  • 31
  • 7
  • Thanks for the answer but I do not think it is relevant to my question. I am not familiar with Google spreadsheet but I would be surprised if it stored dates in USA format. Excel stores dates as the number of days since 31-Dec-1899. My problem, and that of others, is Excel sometimes uses the wrong format when converting that number to a string. I discovered that when a date is entered as dd/mm/yyyy Excel correctly recognises and displays it but if you ask for the NumberFormat you are given mm/dd/yyyy. Excel knows the correct format but gives the wrong format to outsiders. – Tony Dallimore May 28 '12 at 10:36
  • BTW, if your code was Excel VBA, it would not change the value but `.NumberFormat = "dd/mm/yyyy"` would change the appearance if it had been displayed in USA fromat. – Tony Dallimore May 28 '12 at 10:37
  • yeah, my code is for taking ambiguous data and forcing Excel to see it as a data value dd/mm/yyyy. sorry if it's not relevant to your problem. – joshoff Aug 09 '13 at 17:35
0

This question is old, but it's the first that appears on Google when looking for why "VBA NumberFormat is wrong".

It's true that using .Text will make most of the issues of retrieving the displayed content of the cell (except when it actually shows "####..." when the column is too narrow ).

However, if you really need to get the formatting string of a cell as it appears on screen for some reasons, a good starting point is to use .NumberFormatLocal instead which give the number format based on the language of the active user (Documentation).


Also, for "[$-F800]" and "[$-F400]", when they are present they mean that the cell is forced to be respectively the "Long Date" and "Long Time" format as defined in the regional settings of the control panel.

The nice thing is that you can use those names, (ie. Format(CellWithDate, "Long Date")) and it will actually apply the right formatting based on your local configurations.

DecimalTurn
  • 3,243
  • 3
  • 16
  • 36