我正在尝试为国际用户群开发Excel财务模板,以其母语显示工作日名称作为字符串(即今天=“MON”)。对于英语Excel版本,这很简单:=Text(Now(),"DDD")
。但是,我很难找到适用于所有Excel语言的通用解决方案。使用我上面的Text()公式,法国和德国用户在其单元格中获得DDD
个返回值。我也试过=Text(Now(),"*DDD")
,它返回一个不一致的整数,即“07”。
我知道我可以使用4位十六进制参考编号对显示语言进行硬编码。对于德国,这将是=TEXT(NOW(),"[$-0407]DDD")
,其返回令人满意的“Mo”值。显然,这不适用于我的整个全球组,因为我有数百名用户在十几种语言中运行并且不断增长。
是否有动态方式返回本周语言的本地语言日名称?
我目前的“解决方案”是利用选择/工作日函数=CHOOSE(WEEKDAY(NOW(),2),"MON","TUE","..."
来生成本周的英文版本,但这会让我的欧洲用户产生野蛮的愤怒,他们希望他们的语言工作日名称出现。< / p>
VBA选项是可以接受的。感谢。
答案 0 :(得分:2)
返回本周语言名称的动态方式
您可以使用下面的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
附录(评论后编辑)
您可以使用国际国家/地区代码作为ctry
函数中cPattern
参数的默认值,并将其设置为可选(应该是变体,以便能够使用IsMissing
):< / p>
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