I have English Office. Building a macro that builds a string using dates:
Range("G2").Formula = "=TEXT(TRIM(F2),""dd.MM.yyyy"")"
Range("G2:G" & LastRowNum).FillDown
or say
Range("R2").Formula = "=H2&""-""&TEXT(today(),""ddMMyyyy"")&TEXT(now(),""HHmm"")"
Range("R2:R" & LastRowNum).FillDown
I get the correct string.
Colleagues have Office in a different language.
For them, this is unable to build a string. I have to convert my VBA code to reflect their locale.
How do I automatically adjust the formula\VBA to the user's locale, whether it be Portuguese or Russian?
I tried * (asterisk).
Range("R2").Formula = "=H2&""-""&TEXT(today(),""*ddMMyyyy"")&TEXT(now(),""*HHmm"")"
Range("R2:R" & LastRowNum).FillDow
I tried mentioning that formula was built in English, thinking that Excel would "translate" it to user's locale.
Range("R2").Formula = "=H2&""-""&TEXT(today(),""[$-0409]ddMMyyyy"")&TEXT(now(),""[$-0409]HHmm"")"
Range("R2:R" & LastRowNum).FillDow
I have English Office. Building a macro that builds a string using dates:
Range("G2").Formula = "=TEXT(TRIM(F2),""dd.MM.yyyy"")"
Range("G2:G" & LastRowNum).FillDown
or say
Range("R2").Formula = "=H2&""-""&TEXT(today(),""ddMMyyyy"")&TEXT(now(),""HHmm"")"
Range("R2:R" & LastRowNum).FillDown
I get the correct string.
Colleagues have Office in a different language.
For them, this is unable to build a string. I have to convert my VBA code to reflect their locale.
How do I automatically adjust the formula\VBA to the user's locale, whether it be Portuguese or Russian?
I tried * (asterisk).
Range("R2").Formula = "=H2&""-""&TEXT(today(),""*ddMMyyyy"")&TEXT(now(),""*HHmm"")"
Range("R2:R" & LastRowNum).FillDow
I tried mentioning that formula was built in English, thinking that Excel would "translate" it to user's locale.
Range("R2").Formula = "=H2&""-""&TEXT(today(),""[$-0409]ddMMyyyy"")&TEXT(now(),""[$-0409]HHmm"")"
Range("R2:R" & LastRowNum).FillDow
Share
Improve this question
edited Mar 18 at 11:15
CommunityBot
11 silver badge
asked Feb 9 at 10:05
user2858200user2858200
1331 gold badge2 silver badges9 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 0International Date and Time Formatting
- In your first case, it makes sense to have the date format in the formula since when you change a value in the
F
column, the value in theG
column gets updated, although I have no idea howTRIM
fits in. Let us know!? - In your 2nd case, when you use the volatile
TODAY()
andNOW()
functions, it doesn't since the value of time changes on each calculation. But you can still use the functions (out-commented code). It makes sense to use VBA'sFormat
function instead, to return the current time and keep it. - For such simple formulas, you don't need to use the
AutoFill
method. - The code you were trying to utilize (
[$-0409]
or the equivalent[$-en-US]
) has nothing to do with your case. It is used to display months and/or days 'fully' or abbreviated (3 chars) in English no matter the locale. If you want to display the full month name in German on your English system, you could use something like=TEXT(A1,"[$-407]mmmm")
or the equivalent=TEXT(A1,"[$-de-DE]mmmm")
, assuming cellA1
holds a date.
Examples
Sub Test()
Dim LastRowNum As Long: LastRowNum = 5 ' just to compile!
Range("G2:G" & LastRowNum).Formula = "=TEXT(TRIM(F2),""" _
& IntDateFormat("dd.MM.yyyy") & """)"
'Range("R2:R" & LastRowNum).Formula = "=H2&""-""&TEXT(TODAY(),""" _
& IntDateFormat("ddMMyyyy") & """)&TEXT(NOW(),""" _
& IntTimeFormat("HHmm") & """)"
Range("R2:R" & LastRowNum).Formula = "=H2&""-""&" _
& Format(Now(), "ddmmyyyyhhmm")
End Sub
The Functions
- There are probably aspects I didn't consider but the basics are covered. You could certainly dumb them down since you currently don't need the additional functionalities, but they could come in handy when you start tackling conditional formatting.
Function IntDateFormat( _
ByVal DateFormat As String, _
Optional ByVal CorrectDateSeparator As Boolean = False, _
Optional ByVal CorrectDateOrder As Boolean = False) _
As String
Dim SplitDate() As String:
SplitDate = Split(StrConv(DateFormat, vbUnicode), Chr$(0))
ReDim Preserve SplitDate(UBound(SplitDate) - 1)
Dim i As Long
With Application
For i = 0 To UBound(SplitDate)
Select Case SplitDate(i)
Case "Y", "y": SplitDate(i) = .International(xlYearCode)
Case "M", "m": SplitDate(i) = .International(xlMonthCode)
Case "D", "d": SplitDate(i) = .International(xlDayCode)
Case "/"
If CorrectDateSeparator Then
SplitDate(i) = .International(xlDateSeparator)
End If
End Select
Next i
If CorrectDateOrder Then
Select Case .International(xlDateOrder)
'Case 0 ' MDY
Case 1 ' DMY
For i = 0 To UBound(SplitDate)
Select Case SplitDate(i)
Case "M": SplitDate(i) = "D"
Case "m": SplitDate(i) = "d"
Case "D": SplitDate(i) = "M"
Case "d": SplitDate(i) = "m"
End Select
Next i
Case 2 ' YMD
For i = 0 To UBound(SplitDate)
Select Case SplitDate(i)
Case "M": SplitDate(i) = "Y"
Case "m": SplitDate(i) = "y"
Case "Y": SplitDate(i) = "M"
Case "y": SplitDate(i) = "m"
End Select
Next i
End Select
End If
End With
IntDateFormat = Join(SplitDate, "")
End Function
Function IntTimeFormat( _
ByVal TimeFormat As String, _
Optional ByVal CorrectTimeSeparator As Boolean = False) _
As String
Dim SplitTime() As String:
SplitTime = Split(StrConv(TimeFormat, vbUnicode), Chr$(0))
ReDim Preserve SplitTime(UBound(SplitTime) - 1)
Dim i As Long
With Application
For i = 0 To UBound(SplitTime)
Select Case SplitTime(i)
Case "H", "h"
SplitTime(i) = .International(xlHourCode)
Case "M", "m", "N", "n"
SplitTime(i) = .International(xlMinuteCode)
Case "S", "s"
SplitTime(i) = .International(xlSecondCode)
Case ":"
If CorrectTimeSeparator Then
SplitTime(i) = .International(xlTimeSeparator)
End If
End Select
Next i
End With
IntTimeFormat = Join(SplitTime, "")
End Function
Format
function from VBA. It returns results in the local language. – MGonet Commented Feb 9 at 10:32Workbook_Open
macro and andName
'd value), then this solution will work for you. Otherwise, you may need a list of the possible languages. – Ron Rosenfeld Commented Feb 9 at 19:13DAY
,MONTH
andYEAR
functions (along withTEXT
) to create a string of your liking. You might also use theDATE
function to reconstruct the date from your original data and then work with that if you need to use it in a calculation. – Evil Blue Monkey Commented Feb 10 at 22:44