Consulting

Results 1 to 9 of 9

Thread: VBA for Excel: automated correction of wrong decimal separator

  1. #1
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    5
    Location

    VBA for Excel: automated correction of wrong decimal separator

    Hello all,
    1st, let me thank you for the great job you are doing with this forum: I use it a lot to grab examples as I am quite newbie in VBA programming.


    I'd have a request for a sample code to be implemented in a wider spreadsheet.


    I copy and paste a set of data from a webpage to an Excel file.


    The data imported as numbers have some decimal separator which from time to time is not the same of the system where I import the data.


    I would like to create a macro which:


    1 - Detects the decimal separator (comma or full stop) actually used by Excel: the system must work in different versions which can have different separators


    2 - Replaces the commas in the whole spreadsheet with the correct decimal separator



    3 - Replaces the full stops in the whole spreadsheet with the correct decimal separator



    (I don't mind if the action is redundant or not efficient, as I do not operate on a too wide set of data)


    4 - Formats all the columns from D to G, from the second row to the end of the spreadsheet, as numbers


    So that I can later on process the data as numbers.


    I can easily do it manually, but just because I know which is the right decimal separator for my Excel; I need this operation to be automated in order to work on an Excel file of any other person.


    Thank you all again,



    Marco
    Attached Files Attached Files

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: https://www.excelforum.com/excel-pro...separator.html
    Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    5
    Location
    TY Paul. Read the thread. Won't happen again.
    Marco

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()    Dim r, x
        Application.ScreenUpdating = False
        x = Application.DecimalSeparator
        With ActiveSheet
            Set r = Intersect(.Range("D:G"), .UsedRange)
            With r
                .Replace What:=",", Replacement:=x, LookAt:=xlPart
                .Replace What:=".", Replacement:=x, LookAt:=xlPart
                numF = "0" & x & "00;-0" & x & "00;0" & x & "00"
                .Columns("B:D").NumberFormat = numF
                For Each cel In r.Offset(1).Columns(1).Cells
                    If InStr(1, cel, "M") Then
                        cel.Replace What:="M", Replacement:="", LookAt:=xlPart
                        cel.Value = cel.Value * 10 ^ 6
                         cel.NumberFormat = "0" & x & "00,,""M"""
                   End If
                    If InStr(1, cel, "B") Then
                        cel.Replace What:="B", Replacement:="", LookAt:=xlPart
                        cel.Value = cel.Value * 10 ^ 9
                        cel.NumberFormat = "0" & x & "00,,,""B"""
                    End If
                Next
            End With
        End With
        Application.ScreenUpdating = True
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    5
    Location
    TY a lot, mdmackillop, brilliant coding; I would have never guessed in short time.
    I will test it this night and let you know.
    Kind regards,
    Marco

  6. #6
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    5
    Location

    Thumbs up 1 shot - 1 kill!


    Thanks a lot mdmackillop,
    as you probably already know, it worked perfectly!

    Marco

    [This thread can be closed]

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by mdaneri View Post


    [This thread can be closed]
    Above your first post there's [Thread Tools]. One option you can choose is to mark this one solved

    But I see that someone (or you) already did that
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Are you sure that the different Regional Settings for the PC's won't do what you want?

    Simple example - I have my PC set to US Region and entered data.

    Just changing the region to France, Excel very nicely reformatted the way the data was displayed

    France.JPG
    US.JPG

    So maybe import the data with the PC set to the region that generated the data, and then change to your local region

    Just a thought
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    VBAX Newbie
    Joined
    Sep 2017
    Posts
    5
    Location
    Hello Paul,

    thanks for you idea; I had not realised such condition could happen.

    Analysing your picture, it is actually quite improbable as:

    - data will always come in the same form, i. e. with the same format and number of decimal digits before copy and paste to Excel
    - data will be processes only once, after having been copied and pasted in Excel

    Therefore it is difficult to think about changing regional settings while processing. In any case, I imagine that most of the times users manage to do unexpected things, so I will inform them in advance.

    If necessary I will - probably with the help of the community - try to trap this error.

    Thanks again,

    Kind regards,

    Marco


Tags for this Thread

Posting Permissions

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