VBA - Change date language
Asked Answered
S

4

5

I'm trying to solve such problem: In macro, that I'm using, one of the parts is to retrieve date month (in full naming), currently is used :

LastMonth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmmm")

But then one problem appears - for persons, who use different regional language settings, date appears in it's local language, but I need to have it for everyone in English.

I was looking around the internet, but haven't found any similar solutions. Does anyone knows, how can this be solved?

Sadoc answered 19/8, 2015 at 8:29 Comment(1)
Have a look at this similar question: #23406455Batiste
F
15

Excel formula TEXT allows defining output language, so one of the options is to use it's VBA equivalent:

LastMonth = WorksheetFunction.Text(Date - Day(Date), "[$-409]mmmm")
Flanagan answered 19/8, 2015 at 8:40 Comment(1)
Explanation and codes for other languages: excel.tips.net/…Overtrade
B
5

Try this, found it on a forum and it seems to work.

Public Function Format_en(Datum As Date) As String 
Dim DD              As String 
Dim MMM             As String 
Dim YY              As String 

DD = Format(Datum, "dd") 
MMM = Choose(Month(Datum), "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") 
YY = Format(Datum, "yy") 

Format_en = DD & "." & MMM & " " & YY 
End Function 

MsgBox Format_en(Date) 

Reference: http://www.office-loesung.de/ftopic99887_0_0_asc.php

Cheers

Blasto answered 19/8, 2015 at 8:37 Comment(0)
C
1

I'm working in Excel 2016 with German language (don't ask why!). I had to change the date format from 31.12.2023 to 2023-12-31. Unfortunately none of the earlier solutions worked for me but I was able to change the format by changing the language of the date cell:

With wsTemp
    .Range("G2") = start_Date
    .Range("H2") = end_Date
    .Activate
    .Range("G2:H2").Select
    Selection.NumberFormat = "[$-en-IN,1]yyyy-mm-dd;@"  '<-- this the line that worked for me
    .Range("G2:H" & lastRowDestNew).FillDown
End With
Cuirassier answered 12/10, 2023 at 8:12 Comment(0)
M
-1

Just use [$-409] to force English number format.

.NumberFormat = "[$-409]mmmm yy"

gives an output like e.g. "December 19".

.NumberFormat = "[$-409]mmm yy"

gives an output like e.g. "Dec 19".

Moor answered 4/4, 2019 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.