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

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


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


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.