Dynamically Display Weekday Names in Native Excel Language?

放肆的年华 提交于 2019-12-02 04:38:29

问题


I'm trying to develop an Excel financial template for an international user base that displays the weekday name as a string (i.e. today = "MON") in their native language. For English Excel versions, this is easy: =Text(Now(),"DDD"). However, I'm having a hard time finding a common solution that works for all languages of Excel. With my above Text() formula, French and German users are getting DDD return values in their cells. I've also tried =Text(Now(),"*DDD") which returns an inconsistent integer i.e. "07".

I know that I can hard-code the display language with a 4 digit hexadecimal reference number. For Germany, this would be =TEXT(NOW(),"[$-0407]DDD") which returns a satisfying value of "Mo.". Obviously, this fails to work for my entire global group because I have hundreds of users operating in over a dozen languages and growing.

Is there a dynamic way to return the native-language day of the week name?

My current "solution" is to leverage the choose/weekday function =CHOOSE(WEEKDAY(NOW(),2),"MON","TUE","..." to generate the English version of the week, but this is creating barbaric outrage from my European users who want their language weekday name to appear.

VBA options are acceptable. Thanks.


回答1:


Dynamic way to return the native-language day of the week name

You can use the wday function below, calling e.g. the French weekday via wday("fr") to get "Lu" (= lundi). The function uses the international patterns in function cPattern.

VBA - main functions

(1) weekdays

Function wday(ByVal d As Date, ByVal lang As String) As String
' Purpose: get weekday in "DDD" format
'// e.g. Application.Worksheetfunction.Text(date(),"[$-40e]ddd")
wday = Application.WorksheetFunction.Text(d, cPattern(lang) & "ddd")
End Function

(2) months

Function mon(ByVal d As Date, ByVal lang As String) As String
'// e.g. Application.Worksheetfunction.Text(date(),"[$-40e]mmm")
mon = Application.Text(d, cPattern(lang) & "mmm")
End Function

Helper function

Function cPattern(ByVal ctry As String) As String
' Purpose: return country code pattern for functions mon() and wday() 
' Codes: https://msdn.microsoft.com/en-us/library/dd318693(VS.85).aspx
ctry = Trim(LCase(Left(ctry & "  ", 3)))
Select Case ctry
  Case "de"
    cPattern = "[$-C07]" ' German
  Case "en"
    cPattern = "[$-809]" ' English UK
  Case "es"
    cPattern = "[$-C0A]" ' Spanish
  Case "fr",  "fre"
    cPattern = "[$-80C]" ' French
  Case "us"
    cPattern = "[$-409]" ' English US
' more ...
End Select
End Function

Addendum (Edit after comment)

You can use the international Country codes as default value for the ctry argument within the cPattern function and set it optional (should be variant to be able to use IsMissing):

Function cPattern(Optional ByVal ctry As Variant) As String                     ' <<  optional, variant
'
If IsMissing(ctry) Then ctry = Application.International(xlCountrySetting) & "" ' << ADD if no ctry Definition
If Len(ctry) = 0 Then ctry = Application.International(xlCountrySetting) & ""
ctry = Trim(LCase(Left(ctry & "  ", 3)))
Select Case ctry
'
Case "43", "de"         ' << add individual Country Codes
   cPattern = "[$-C07]" ' German
' ...
End Select
End Function

In a similar way you should change the 2nd Argument in the wday function optional and variant:

Function wday(ByVal d As Date, optional ByVal lang) As String
If IsMissing(lang) then lang = ""   ' << if 2nd arg is missing then empty string
wday = Application.WorksheetFunction.Text(d, cPattern(lang) & "ddd")
End Function

2nd Edit

Generally an empty pattern prefix would automatically display English writing, but this is redirected in the helper function wday by defining additional country settings (see cPattern function above).

You could change the main functions as follows to include the DDD formatting:

'(1) weekdays
Function wday(ByVal d As Date, Optional ByVal lang) As String
' Purpose: get weekday in "DDD" format
' ----------------------------
' I. If 2nd argument is missing, then use local writing
' ----------------------------
  If IsMissing(lang) Then         ' missing 2nd argument
     wday = Format(d, "ddd")
' ----------------------------
' II. If 2nd argument exists, then search language code prefix to get any defined language
' ----------------------------
  Else                            ' 2nd argument exists
  '// e.g. Application.Worksheetfunction.Text(date(),"[$-40e]ddd")
      wday = Application.WorksheetFunction.Text(d, cPattern(lang) & "ddd")
  End If
End Function


来源:https://stackoverflow.com/questions/48155382/dynamically-display-weekday-names-in-native-excel-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!