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
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")
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)
.
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
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.
I didn't actually know the Formula
s 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 howApplication.DecimalSeparator
andApplication.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.
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