简体   繁体   中英

Excel decimal separator behaves differently based on regional setting

I have an excel which there are some values with decimals. The values are strings , and since many countries are using it, some countries use "," comma for decimal places, others use "." dot for decimal places.

Once all the data worldwide is compiled, people from different regional areas process the data with macros.

In order for people to use same settings, I change in the macro their decimalseparator to be all the same:

Application.UseSystemSeparators = False
Application.DecimalSeparator = ","
application.ThousandsSeparator = "."

I then run certain macros to add the values together and this is where the problem begins. For people in europe the macro works perfectly. The macro first converts any dots to commas like this (note that the cell value is a string):

If InStr(NW.Cells(ttt, 100), ".") > 0 Then
       x2 = Replace(NW.Cells(ttt, 100), ".", ",")
       NW.Cells(ttt, 100) = x2
End If

So if NW.Cells(ttt, 100) = 12.25 it converts it to 12,25 then it runs further macros (adding all values together).

But if a person in USA runs the macro, the 12.25 becomes 1225

Since I have changed the application.decimalsepator to "," at the beginning of the macro, I don't understand why the macro behaves differently...

Any ideas how to solve this ?

Note Excel 2007 and Excel 2010 is being used so cannot use the =NUMBERVALUE function...

thanks

Sub changeSeparators()
Dim myRange As Range
Dim rng As Range

Set myRng = ActiveSheet.Range("A1:A500")

For Each rng In myRng
   rng.Value = Replace(rng.Value, "'", ",")
   If Left(Right(rng.Value, 3), 1) = "," Then
       rng.Value = Mid(rng.Value, 1, Len(rng.Value) - 3) & "." & Right(rng.Value, 2)
   End If
Next rng
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM