+ Reply to Thread
Results 1 to 15 of 15

Weird decimal separator issue

  1. #1
    Registered User
    Join Date
    05-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    37

    Question Weird decimal separator issue

    I have an Excel file that is generated from an SQL db, through an OpenXML request.
    All fields are exported as strings.

    Depending on the customer's configuration, sometimes the decimal separator is a comma, sometimes a point.

    When the Excel file is first open, some code runs to clean and normalize the data, especially to convert numbers and dates properly.
    I check for the local decimal separator (from the regional settings) and replace whatever separator is used in the file with it.

    This is where I have a problem.

    Let's assume my separator is a comma.
    If the file already has commas, then it's fine, nothing changes.
    If it's a point, sometimes it works, sometimes not.

    If the number I want to convert has 2,3,4 decimals, it converts fine.
    More than that and it goes crazy.

    A number like 32.12345678 will be converted to 3212345678 (the point is not replaced, it is even removed!)
    The same number but 32.12 will be converted to 32,12 just fine.

    This is driving me crazy. By the way I used the replace function to change the string in complete ranges, much faster.

    Any help is appreciated!!

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Weird decimal separator issue

    Hello joebanana,

    Welcome to the Forum!

    I am guessing your forgot the step of disabling the System Separators after you changed Excel's. Here is how the code should look...
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    05-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Weird decimal separator issue

    Hi Leith,

    Thanks for your reply. Indeed I am not changing Excel's separators.
    Where exactly should I put the code above?

    At the beginning of the procedure?

    Can I set UseSystemSeparators back to true when the procedure has completed?

    Thanks again, very appreciated.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Weird decimal separator issue

    Hello joebanana,

    You can place the code at the beginning of the procedure and yes, you should set the SystemSeparators to true when you are done.

  5. #5
    Registered User
    Join Date
    05-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Weird decimal separator issue

    Unfortunately it is still the same.

    Maybe I can explain better... below is an example of the data I have:

    Column A
    3,03
    3002,45

    Column B
    3,0303030303030303
    3002,459868482784723

    The decimal separator is the same, as you can see.
    When I use the replace function

    Please Login or Register  to view this content.
    This is where it fails. There is no . in the cell, yet Excel somehow removes it, thus converting my data to something like 3030303030303.
    It works just fine in the column where I have 2 decimals.

    So it has something to do with the numbers of decimals...

    I'm puzzled, really.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Weird decimal separator issue

    Hello Joe,

    Can you post a sample of the data and provide some before and after examples? This will clear up a lot a questions for me quickly and get you the answer you need.

    To Attach a File:

    1. Scroll down to the window below your post Additional Options
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.

  7. #7
    Registered User
    Join Date
    05-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Weird decimal separator issue

    There you go.

    To have the code run, change the value in the Settings tab from No to Yes.

    Thanks again!
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    05-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Weird decimal separator issue

    By the way here is the source data if you need to "play" with it.
    Attached Files Attached Files

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Weird decimal separator issue

    Hello Joe,

    Thanks for posting the workbook. Since you are in Quebec, you're dealing with both English and French number styles. Are you also running English and French versions of Office? Will the macro be used by both?

  10. #10
    Registered User
    Join Date
    05-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Weird decimal separator issue

    That's an annoyance indeed.

    My file needs to be compatible with Excel 2007 and 2010, English and French.
    This is why the decimal separator is not always the same when the data is output from the SQL database.

    Also, I MUST display the data, once cleaned, in the locale format.

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Weird decimal separator issue

    Hello Joe,

    That's what I thought. I will need to make some changes to the macro to detect the Excel version and the number formatting.

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Weird decimal separator issue

    Hello Joe,

    You're right, this is crazy. There must be something causing the inconsistent behaviour when the data is either imported or cleaned. I have not been able to track down the cause yet. I have changed the Application settings, the Regional settings, examined individual cell formats, and nothing obvious stands out as to why some cell formats change and some don't.

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Weird decimal separator issue

    Hello Joe,

    Found it! It is the importing causing the problem. I verified it by using the CELL worksheet function. Setting the Info_Type string to "type" reveals that some cells are Labels. Although they look like numbers have a General format, Excel regards them as Text!

    For example, using the "Data" sheet, cell K2 will return a "v" (Value) and cell O2 will return "l" (Label). This why some cells, the ones with values, change and other remain the same. Good news is the macro can check the cells and convert them automatically and set the number format according to the locale.

  14. #14
    Registered User
    Join Date
    05-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Weird decimal separator issue

    Leith,

    I'm home now and won't be able to try again until Monday, but I really hope you're right.

    You are mentioning Excel reads the cells as Text, but this is correct. Well, not correct, but known.

    The Excel file is populated from an SQL db using OpenXML, and OpenXML writes everything in Excel as STRING.
    My Excel file is pre-formatted, meaning I already formatted my columns to whatever they're going to be eventually (a number, a date, a text).

    Normally, both columns that show Power Plan Usage should be set as number.

    Either way, does it explain why Excel removes the comma when I ask it to replace a point?

  15. #15
    Registered User
    Join Date
    05-25-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    37

    Re: Weird decimal separator issue

    I managed to resolve the issue by having my DBA modify his OpenXML routine to export the fields the way they should.
    They are now directly recognized by Excel and I no longer need to waste my time converting them.
    Thanks all for your comments!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1