使用VBA将区域格式更改为另一种语言

时间:2018-07-31 18:42:53

标签: excel vba excel-vba

我需要将区域格式更改为“法国(加拿大)” ,以使master接受法国月份(例如,火星05,艾薇儿12等),然后< strong>在VBA代码中将其还原为“英语(加拿大)” 。

我希望它不会那么复杂,并且有一个写属性可以使用VBA修改此设置。

到目前为止,我已经找到了Application.International(xlCountrySetting),但这只是一个读取属性。

这是我希望更改的设置:enter image description here enter image description here

谢谢

1 个答案:

答案 0 :(得分:2)

正如注释中指出的,您可以通过几个简单的Windows API调用来完成此操作。建议不要使用GetUserDefaultLCID测试当前设置,而不要假设机器当前设置为“英语(加拿大)”。

#If VBA7 Then
    Private Declare PtrSafe Function SetThreadLocale Lib "kernel32" _
        (ByVal Locale As Long) As Boolean
    Private Declare PtrSafe Function GetUserDefaultLCID Lib "kernel32" () As Long
    Private Declare PtrSafe Function LocaleNameToLCID Lib "kernel32" _
        (ByVal lpName As LongPtr, dwFlags As Long) As Long
#Else
    Private Declare Function SetThreadLocale Lib "kernel32" (ByVal Locale As Long) As Boolean
    Private Declare Function GetUserDefaultLCID Lib "kernel32" () As Long
    Private Declare Function LocaleNameToLCID Lib "kernel32" _
       (ByVal lpName As LongPtr, dwFlags As Long) As Long
#End If

Private Sub Test()
    'Get the locale identifier for French (Canada)
    Dim frCa As Long
    frCa = LocaleNameToLCID(StrPtr("fr-CA"), 0)
    'Make sure there function succeeded.
    If result = 0 Then
        'Cache the current locale
        Dim userLocale As Long
        userLocale = GetUserDefaultLCID
        'Switch to French (Canada)
        If SetThreadLocale(frCa) Then
            'en français
            '...
            'switch back
            SetThreadLocale userLocale
        End If
    End If
End Sub

相关功能的文档链接如下: