Using VBA to detect which decimal sign the computer is using

前端 未结 6 1373
你的背包
你的背包 2020-11-30 12:54

Is it possible to use VBA to detect which decimal sign is being used on the computer?

I have a macro script that adds a conditional formatting to an excel sheet. The

相关标签:
6条回答
  • 2020-11-30 12:59

    For applications other than Excel, the solution in the accepted answer is not available.

    Instead, you can use Format to retrieve the decimal separator: an unescaped dot in Format gets replaced by the current decimal separator.

    DecimalSeparator = Format(0, ".")
    

    You can also look up the decimal separator from the registry

    DecimalSeparator = CreateObject("WScript.Shell").RegRead("HKCU\Control Panel\International\sDecimal")
    
    0 讨论(0)
  • 2020-11-30 13:01

    Regarding the answer above, it is important to know that Application.DecimalSeparator and Application.International(xlDecimalSeparator) do not behave the same way:

    • Application.DecimalSeparator will ALWAYS output the decimal separator chosen in Excel options even when Excel is told to use System Separators (from Windows regional settings)
    • Application.International(xlDecimalSeparator) will output whatever is the actual decimal separator used by Excel whether it comes from Windows settings (when Application.UseSystemSeparators = True) or from Excel options (when Application.UseSystemSeparators = False)

    I therefore strongly recommend to always use Application.International(xlDecimalSeparator).

    0 讨论(0)
  • 2020-11-30 13:07

    My 2 cents here mixing the Excel answers here and the awesome registry trick from Erik A.; but I want to include Word in this game, because I use to automate Word/Outlook a lot:

    Function CorrectDecimalSeparator() As String
    Dim auxCorrectListSeparator As String
        If WordIsOpen Then
            CorrectDecimalSeparator= Word.Application.International(wdListSeparator)
        ElseIf ExcelIsOpen Then
            CorrectDecimalSeparator= Excel.Application.International(xlListSeparator)
        Else
            auxCorrectListSeparator = CreateObject("WScript.Shell").RegRead("HKCU\Control Panel\International\sDecimal")
        End If
    End Function
    Function WordIsOpen() As Boolean
        Dim oWord As Object
        On Error Resume Next
        Set oWord = GetObject(, "Word.Application")
        On Error GoTo 0
        WordIsOpen = Not oWord Is Nothing
        Set oWord = Nothing
    End Function
    Function ExcelIsOpen() As Boolean
        Dim oExcel As Object
        On Error Resume Next
        Set oExcel = GetObject(, "Excel.Application")
        On Error GoTo 0
        ExcelIsOpen = Not oExcel Is Nothing
        Set oExcel = Nothing
    End Function
    
    0 讨论(0)
  • 2020-11-30 13:15

    You can use the DecimalSeparator property.

    Application.DecimalSeparator then returns the decimal separator defined by the locale that excel is being run with.

    On a side note: It's advisable, even though it's possible, to not change this and instead leverage it to your needs.

    0 讨论(0)
  • 2020-11-30 13:21

    I didn't actually know the Formulas in FormatConditions accept localized formulas. In other places you have a choice between Formula and FormulaLocal.


    Please note:
    This part turned out to be oversimplified to the point of being wrong. Please refer to the other answer (which should really have been the accepted one) for how Application.DecimalSeparator and Application.International(xlDecimalSeparator) actually behave.

    To simply answer the question, you can use Application.International(xlDecimalSeparator) or simply Application.DecimalSeparator to know the separator.


    But for non-trivial formulas it might be easier to assign the invariant English-locale based formula to the Formula property of a hidden cell and then read FormulaLocal from that cell and use that for FormatConditions. Excel will do all the conversions for you.

    0 讨论(0)
  • 2020-11-30 13:23

    You can also convert a known number derived from a calculation to a known string and thenextract the separator. In this example, 1/2 evaluates to a three-character string "0.5" or "0,5". The Mid function extracts 1 character from positon 2 in the string.

    Public Function DecimalSeparator() As String
        DecimalSeparator = Mid$(1 / 2, 2, 1)
    End Function
    
    0 讨论(0)
提交回复
热议问题