以本地 Excel 语言动态显示工作日名称?

Dynamically Display Weekday Names in Native Excel Language?

我正在尝试为国际用户群开发一个 Excel 财务模板,以他们的母语将工作日名称显示为字符串(即今天 = "MON")。对于英文 Excel 版本,这很简单:=Text(Now(),"DDD")。但是,我很难找到适用于 Excel 所有语言的通用解决方案。使用我上面的 Text() 公式,法国和德国用户在他们的单元格中获得 DDD return 值。我也尝试过 =Text(Now(),"*DDD"),其中 return 是一个不一致的整数,即“07”。

我知道我可以使用 4 位十六进制参考号对显示语言进行硬编码。对于德国,这将是 =TEXT(NOW(),"[$-0407]DDD"),其中 return 是 "Mo." 的令人满意的值。显然,这对我的整个全球团队都不起作用,因为我有数百名用户使用十几种语言并在不断增长。

是否有一种动态方式来return母语星期几名称?

我目前的 "solution" 是利用 choose/weekday 函数 =CHOOSE(WEEKDAY(NOW(),2),"MON","TUE","..." 生成英文版的一周,但这引起了我想要他们的语言的欧洲用户的野蛮愤怒要显示的工作日名称。

VBA 选项是可以接受的。谢谢。

return母语星期名称的动态方式

您可以使用下面的 wday 函数,例如调用法语平日通过wday("fr")得到"Lu"(=lundi)。该函数在函数 cPattern 中使用了国际模式。

VBA - 主要功能

(1) 个工作日

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)个月

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

辅助函数

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

附录(评论后编辑)

您可以使用国际国家代码作为 cPattern 函数中 ctry 参数的默认值,并将其设置为可选(应该是变体才能使用 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

以类似的方式,您应该更改 wday 函数中的第二个参数可选和变体:

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

第二次编辑

一般情况下,空模式前缀会自动显示英文书写,但这会在辅助函数 wday 中通过定义其他国家/地区设置进行重定向(请参阅上面的 cPattern 函数)。

您可以如下更改主要功能以包含 DDD 格式:

'(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