How to prevent Excel to use the OS regional settings for date patterns in formulas

114,722

Solution 1

This question is a bit old, but to date it does not seem to have an answer. I've also seen similar questions on a number of sites, but have not yet found any answer that involves only built-in Excel functions. However, it is quite easy to solve this with VBA. You don't even have to know how to program to do this, because the required function is so simple.

With your workbook open, simply press Alt+F11 (to open the VBA editor) Then Click the menu item Insert > Module In the new VBA module, enter the following:

Public Function FMT$(ByVal Value, ByVal strFormat)
    FMT = VBA.Format$(Value, strFormat)
End Function

What this does is simply use VBA's own Format function instead of Excel's TEXT function. This is exactly what you want.

This new function will format any date (or number) according to whatever format string you specify. It will interpret the format string using the standard (en-US) notation, regardless of the operating system's regional settings.

To use this in your workbook, simply type =FMT(A1, "yyyymmdd_hhss") instead of =TEXT(A1, "yyyymmdd_hhss"). (Of course you can change the cell reference and format string as necessary.)

By the way, you can name the function whatever you want. I chose FMT because it was short and because the function can format both numbers and dates. But you can choose something more descriptive if you want. Just remember to use the same name in your worksheet as in the VBA code.

Note that the VBA format strings are not exactly the same as Excel's custom format strings (for example, use "n" instead of "m" for minutes), but they are very similar. Look here for details, or search MSDN for "Format Function (Visual Basic for Applications)". Scroll to the bottom to see the various date format specifiers.

Method 2

Another approach that also uses VBA, but might actually be easier to maintain, is the following:

  1. Apply your desired date format to a cell, using the normal Cell Format dialog. This cell can be on a hidden sheet if you prefer--it does not need to be displayed to the end-user. Let's assume you applied the format yyyymmdd\_hhss to cell $A$1 (note that the underscore must be escaped as shown).
  2. Add the GetFormat function (shown below) to a VBA module in your workbook.
  3. Enter =GetFormat($A$1, TRUE) into another cell (e.g. $B$1)
  4. That function will return the localized version of the format string. So even though you originally formatted $A$1 with yyyymmdd\_hhss, when you open the workbook on a computer using the French language (for example), the function will show aaaammjj\_hhss.
  5. Now simply reference the second cell in all of your TEXT functions. For example: =TEXT(F22, $B$1).

Here's the VBA code:

Public Function GetFormat$(Cell As Range, Optional ByVal UseLocal As Boolean)
    If UseLocal Then
        GetFormat = Cell.NumberFormatLocal
    Else
        GetFormat = Cell.NumberFormat
    End If
End Function

This allows us to "inspect" the cell you originally formatted ($A$1) to retrieve the localized format-string. That string will represent the same format you applied, but it will use the correct letters for TEXT to interpret ("j" instead of "d" for example), so the displayed value of the dates will be constant across all locales. If you wanted to use only one date format for your whole workbook, you would only need to do steps 1-4 once. Then repeat step 5 (the TEXT function) in all the cells where you currently use it, but instead of hard-coding a format, you would simply reference the cell that contains the localized format-string ($B$1 in the example instructions).

Note that the second argument to GetFormat tells the function whether or not to return the localized version (which depends on regional settings) or the "standard" version (which is always based on en-US).

Here are some screen-shots that might make this more clear.

en-US

  • In the figure, Column 1 lists several representations of a single date with different formats applied.
  • Note that rows 2 and 3 use Excel's "system default" date formats. (These are indicated by a leading asterisk in the Format Dialog and they indicate that the user's default date format should be used.) Also note that row 5 uses a bracketed LCID, which forces the language used for month and day names to be English (different LCIDs can be used to specify other languages).
  • The second column shows the result of GetFormat(Cell, FALSE) for each cell in column 1. (Recall that FALSE for the second parameter causes the function to return the NON-localized formats).
  • The third column shows what GetFormat(Cell, TRUE) returns for each cell in column 2. (i.e. the localized formats).
  • The fourth column shows the result of the TEXT function using the original, raw date value and the localized result of GetFormat to re-produce the format shown in column 1. Note however that NO number formatting was applied to this column. The values are a direct result of the TEXT function.

The results for the English (US) case above are not very interesting, but you can compare them with the following results that were obtained by changing the Regional Settings of my operating system to various other locales. Importantly, note that by using GetFormat in combination with TEXT we are able to retain a constant result for numeric formats (those that do not include day or month names) across all locales. And by constraining the language using an LCID (as in row 5) we can even retain a constant format when including day and month names as well.

fr-CH

nb-NO

ru-RU

This method works for most locales, however it should be noted that the scripts used to represent the Hindu-Arabic numerals are NOT the same in all locales. Therefore regional settings like those of the Nepali (India) locale will result in date formats that "look" different than en-US dates. But these actually are in the same "format" in terms of the positions of numbers--they just use different symbols for the numbers.

ne-IN

Solution 2

For dates you can actually define a format that makes Excel use a certain locale. If you set the number format of the cell containing your date to something like

[$-409]m/d/yy h:mm AM/PM;@

Excel will display your date in a US locale (hex 409)

Gotcha: The YMD notation is localized and thus turns to JMT on a german system for example. So you might have to adapt the format string to your situation but i would expect it to behave like the formulas (autotranslate) on systems using a different language (as you seem to be aware of).

See here for some more details: https://stackoverflow.com/questions/894805/excel-number-format-what-is-409

and here for a list of locales: http://support.microsoft.com/kb/221435

Solution 3

Even though this question is a bit old, I came across another (simple!) solution and am posting here for the sake of completeness/reference.

  • I tried ricovox's answer successfully, but wanted a solution without VBA.

  • Tested MarinD's solution as well. But, as pointed out in the comments, if you need to cater for more than one country-specific format it can get unwieldy pretty fast.

  • And now to my solution: using Excel's native YEAR(), MONTH() and DATE() functions, wrapped in TEXT() to pad the zeros in front. With the Date in A1:

    =TEXT(YEAR(A1),"0000")&"-"&TEXT(MONTH(A1),"00")&"-"&TEXT(DAY(A1),"00")

    gives me the date in YYYY-MM-DD format, irrespective if the current locale requires YYYY-MM-DD or AAAA-MM-JJ or any other localised names for year/month/date.

    Excel translates its native formulae automatically.

Solution 4

A simple way to solve the Excel locale date format problem is to use the CHOOSE function:

CHOOSE(WEEKDAY(TODAY());"Dimanche";"Lundi";"Mardi";"Mercredi";"Jeudi";"Vendredi";"Samedi")

CHOOSE(MONTH(TODAY());"Janvier";"Fevrier";"Mars";"Avril";"Mai";"Juin";"Juillet";"Août";"Septembre";"Octobre";"Novembre";"Décembre")

In these examples you will get the Day and Month in letters without using locale formats like "Mmmm" or "Dddd" or "Jjjj".

Solution 5

If your reference date is in cell A1:

=IF(RIGHT(TEXT(A1;"dd-mm-yyyy");1)="y";TEXT(A1;"jj-mm-aaaa");TEXT(A1;"dd-mm-yyyy"))
Share:
114,722

Related videos on Youtube

рüффп
Author by

рüффп

More than 20 years experience in IT consulting in different business and using different technologies. My main IT interest are: Docker and containers Cloud technos Security Spring framework Open source projects in general (Apache, Spring) Programming languages (Java, go, python, C++, C) I have many other interests like biology and nature, birdwatching, photography, diy, home electronics.

Updated on September 18, 2022

Comments

  • рüффп
    рüффп over 1 year

    According to this question I have the following problem:

    I want to use some Excel function (not the cell formatting) like TEXT(A1, {date_pattern})

    But the person who answer my previous question make me found that the date pattern change according to the Windows Regional Settings.

    However, my OS (Windows 7) is in English and the Office suite as well. By looking in my Regional settings it show even a pattern using English notation (dd.MM.yyyy)

    Region and Language

    I want to know if there is any way to disable such behaviour from Excel, meaning I want to always use the English patterns and never the localized ones because I do not want the behaviour of my Excel sheet to change according to localisation of the reader.

    A simple case would be reformatting some date field to a computer centric way like this: "yyyymmdd_hhss" this is recognized universally and can be sorted up and down easily. But as I am in the French part of Switzerland part I should write "aaaammjj_hhss" and if I send this Excel to a colleague in Zürich he would not be able to see the proper date as he got the Swiss German localization (his excel would expect "jjjjmmtt_hhss")

    We were clever enough to install all windows and office in English but we still face problem like this because this link to the OS regional settings.

    For me the changing Windows Settings is not an option because all the other programs are using this settings.

  • рüффп
    рüффп about 10 years
    But it does not answer my question. I want to be able to write dd/mm/yyyy in my Excel sheet even the OS wants "jj/mm/aaaa". I tried to change my OS locale and the output is changing, then Excel does not adapt according the user's Locale. If I write "jj.mm.aaaa" (as in French) and I change my locale to be US, the Excel shows: "jj.03.aaaa" which is not adapted.
  • рüффп
    рüффп about 9 years
    That is a good workaround if you need to manage only one extra format... I am sure anyway my colleagues from Zurich or any other country which is not english nor french, this will still not work. If you have to make if(if(if... for every single format all around the world, it would be very difficult to manage.
  • MarinD
    MarinD about 9 years
    That's right, but I could not find anything better without going through a VBA macro. I don't understand why Microsoft translate the functions and their properties (which is even worse because they do not translate them automatically when you open it with another language, as in our issue here). No programming language ever did that. Just English would have been enough
  • рüффп
    рüффп almost 9 years
    Thanks a lot for your answer (even late) - Method 1 is the best for me and simple enough for all my cases. Very good and complete answer.
  • рüффп
    рüффп almost 9 years
    Simple as long you are managing one language (French in your case) but if you have to manage multiple anguages, your formula will become way too big.
  • Alexis PERROTTEY
    Alexis PERROTTEY almost 9 years
    I manage only one langage. The point of the use of CHOOSE is precisely to produce a one langage Excel sheet that will work on any other locale version of Excel.
  • Alexis PERROTTEY
    Alexis PERROTTEY almost 9 years
    The big failure of Excel TEXT function is that the date format depend on your current Windows locale in Regional Settings. Example = =TEXT(TODAY();"YYYY-MM-DD") for US =TEXT(TODAY();"AAAA-MM-JJ") for FR So it is not possible to create a universal Excel sheet using TEXT. Use CHOOSE instead.
  • рüффп
    рüффп over 8 years
    At the end, a solution with another function with hard-coded values is not really helping more than hard-coding the date patterns. Personally I would go with VBA and the accepted answer. Thanks anyway for sharing your experience.
  • Ilan
    Ilan over 8 years
    Could you plz explain how can I insert 28.11.2015 using the first method? Where and what should I write???????
  • Burgi
    Burgi about 8 years
    Can you expand your answer to include an example?
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' over 6 years
    You say this works in Switzerland, but is it “French (Switzerland)”, “German (Switzerland)”, or is it English?  If it’s English, this doesn’t really contribute anything to this discussion.  If it’s not English, can you explain why =TEXT(C1,dateformat) would work (with the dateformat cell containing dd/mm/yyyy) when =TEXT(C1, "dd/mm/yyyy") doesn’t work?  (Or does it work?)
  • Scuba Steve
    Scuba Steve almost 6 years
    In Excel 2016, it will keep whatever regional formatting was assigned to it. If you're trying to compare dates with different regional formats, the comparison will fail. This is so bad it's probably a bug.
  • Ister
    Ister about 5 years
    It might not work. I don't know all languages but as long as the word for month doesn't start with "m" in some of the languages, this will probably not work at all (rendering a #VALUE error). Even if the word starts with "m" there are more than just 2 options here. Germany, Poland and Hungary each have a different formatting codes to name just a few.
  • GWD
    GWD about 5 years
    I assume hat the developer knows what language setting he is developing in (e.g. here English system) and which he wants his formula to work in/to translate it to. Should be no big deal to extend the IF for whatever language he/she is interested in.
  • Ister
    Ister about 5 years
    Well, I've listed those specific countries because I already had a case where I had to adapt to all those languages at the same time. There is also a limit of embedded IFs, however this is also easy to sidestep. I agree that the idea is good in general and its main advantage is no VBA needed.
  • Jack Miller
    Jack Miller over 3 years
    Just tried this formular with a German Excel installation for the 2nd Februar 2004. It gives me: "00or2 Da22004"