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
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]ffffd")
wday = Application.WorksheetFunction.Text(d, cPattern(lang) & "ffffd")
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) & "ffffd")
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, "ffffd")
' ----------------------------
' 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]ffffd")
wday = Application.WorksheetFunction.Text(d, cPattern(lang) & "ffffd")
End If
End Function