Application.DecimalSeparator问题

时间:2015-03-19 18:39:55

标签: excel vba excel-vba

我的区域使用“,”作为小数点分隔符。当我在Excel 2013中运行以下代码以导入使用“。”的数字时。作为小数分隔符我没有错误。

Dim strGetResult As String
strGetResult = httpObject.responseText
strGetResult = Replace(strGetResult, ".", Application.DecimalSeparator)

但是当我在Excel 2010中尝试使用“,”作为小数点分隔符的相同代码时,我得不到相同的结果。调试显示Application.DecimalSeparator =“。” 这没有任何意义,因为数字在Excel中输入了“,”分隔符。

知道为什么会这样吗?

3 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,并设法在this thread中找到答案。

  

Application.DecimalSeparator仅在用户时才相关   选中了UseSystemSeparators选项。

例如,如果Application.UseSystemSeparators = True,则Application.DecimalSeparator的值不会反映Excel实际使用的内容。如果在MSDN docs for Application.DecimalSeparator中描述了这一点会很好,但事实并非如此。

mrexcel线程还包含一个优雅的解决方案;而不是调用Application.DecimalSeparator,创建一个函数

Public Function GetDecimalSeparator()

    GetDecimalSeparator = Mid(Format(1000, "#,##0.00"), 6, 1)

End Function

然后打电话给那个。我使用这种方法似乎工作正常。

答案 1 :(得分:1)

我的区域也使用“,”作为小数点分隔符和“。”千位分隔符。我过去也遇到过类似的问题。不幸的是,我现在无法重复错误,但我记得更改Excel的小数分隔符也没有解决问题。

我能找到的唯一解决方法(我并不喜欢)是在Workbook_Open中按代码切换十进制和千位分隔符,并将它们更改回Workbook_BeforeClose中的原始分隔符。其中很大一部分是工作簿继续显示原始分隔符,而代码使用了更改的分隔符。这样用户就不会看到任何差异。

我想您可以使用此技术将区域设置更改为httpObject使用的设置,仅用于导入过程并消除代码的替换部分。

此处找到原始解决方案:http://www.xtremevbtalk.com/archive/index.php/t-138511.html

将下面的代码放在标准模块中。调用InitLocale将小数分隔符设置为“。”千位分隔符“,”。调用RestoreLocale以恢复原始设置。两个命名范围(rDecimal和rThousand)用于存储原始设置,但如果您在同一过程中更改回原始设置,则可能不需要这样做。

Declare PtrSafe Function GetLocaleInfo Lib "kernel32" Alias "GetLocaleInfoA" _
(ByVal LOCALE As Long, ByVal LCType As Long, ByVal lpLCData As String, ByVal cchData As Long) As Long
Declare PtrSafe Function GetUserDefaultLCID% Lib "kernel32" ()
Declare PtrSafe Function SetLocaleInfo Lib "kernel32" Alias "SetLocaleInfoA" _
(ByVal LOCALE As Long, ByVal LCType As Long, ByVal lpLCData As String) As Long
Public Const LOCALE_SCURRENCY = &H14
Public Const LOCALE_SDECIMAL = &HE
Public Const LOCALE_STHOUSAND = &HF

Public SDECIMAL As String
Public STHOUSAND As String
Public LOCALE As Long
Public rDecimal As Range, rThousand As Range


Sub FixLocale()
    Set rDecimal = ActiveWorkbook.Names("sDecimal").RefersToRange
    Set rThousand = ActiveWorkbook.Names("sThousand").RefersToRange
    Call GetLocale
    rDecimal = SDECIMAL
    rThousand = STHOUSAND
    If SDECIMAL <> "." Or STHOUSAND <> "," Then
        Call SetLocale(".", ",")
    End If
End Sub

Sub RestoreLocale()
    Set rDecimal = ActiveWorkbook.Names("sDecimal").RefersToRange
    Set rThousand = ActiveWorkbook.Names("sThousand").RefersToRange
    Call GetLocale
    If SDECIMAL <> rDecimal Or STHOUSAND <> rThousand Then
        Call SetLocale(rDecimal.Value, rThousand.Value)
    End If
End Sub

Sub GetLocale()
    Dim Symbol As String
    Dim iRet1 As Long
    Dim iRet2 As Long
    Dim lpLCDataVar As String
    Dim Pos

    LOCALE = GetUserDefaultLCID()

    iRet1 = GetLocaleInfo(LOCALE, LOCALE_SDECIMAL, lpLCDataVar, 0)
    Symbol = String$(iRet1, 0)
    iRet2 = GetLocaleInfo(LOCALE, LOCALE_SDECIMAL, Symbol, iRet1)
    Pos = InStr(Symbol, Chr$(0))
    If Pos > 0 Then
        SDECIMAL = Left$(Symbol, Pos - 1)
    Else
        MsgBox ("Error geting LOCALE")
    End If

    iRet1 = GetLocaleInfo(LOCALE, LOCALE_STHOUSAND, lpLCDataVar, 0)
    Symbol = String$(iRet1, 0)
    iRet2 = GetLocaleInfo(LOCALE, LOCALE_STHOUSAND, Symbol, iRet1)
    Pos = InStr(Symbol, Chr$(0))
    If Pos > 0 Then
        STHOUSAND = Left$(Symbol, Pos - 1)
    Else
        MsgBox ("Error geting LOCALE")
    End If

End Sub

Sub SetLocale(SymbDecimal As String, SymbThousand As String)
    LOCALE = GetUserDefaultLCID()
    iRet1 = SetLocaleInfo(LOCALE, LOCALE_SDECIMAL, SymbDecimal)
    LOCALE = GetUserDefaultLCID()
    iRet1 = SetLocaleInfo(LOCALE, LOCALE_STHOUSAND, SymbThousand)
End Sub

答案 2 :(得分:1)

As stated by GodSmith,如果Application.UseSystemSeparators = True,则Application.DecimalSeparator返回不正确的值。但你可以使用:

Application.International(xlDecimalSeparator)

找到实际的小数分隔符。