简体   繁体   中英

Location causes incorrect placement of separators in Excel during file import

I import large amounts of data into Excel. These are previously reduced in quantity from 100 Hz to 1 Hz by a third-party program to reduce work and load time. However, during this reduction process, decimal and thousands separators are swapped, probably because the software is designed in a different language.

Original (Example line):

009 090308.510 +2475.77145123 -0091.51682637 070.530 271.89 +0168.67 +0001.13 -8.485680E-04 0.000000 +4.625850E-04 +2.679440E+36 -2.544081E-29 +2.658468E+36

Processed by third party program:

009 090308,510 +2475,77145123 -0091,51682637 070,530 271.89 +00168,67 +001,130 0,000000 -8.485680E-04 +4.625850E-04 +2.679440E+36 -2.544081E-29 +2.658468E+36

As can be seen, some separators are swapped by the program, but others are not. If I now apply my import code to both formats, I get the following results:

Original:

9 90308.51 2475.771 -91.5168 70.53 271.89 168.67 1.13 -8.49E-04 0 4.63E-04 2.68E+36 -2.54E-29 2.66E+36

Processed:

9 90,308,510 247,577,145,123 -9,151,682,637 40,530 271.89 +00168,67 1,130 0,000000 -8.49E-04 4.63E-04 2.68E+36 -2.54E-29 2.66E+36

For understanding here the code for import:

Option Explicit

Public Sub fileImporter()
    Dim fDialog As FileDialog
    Dim fPath As Variant
    Dim FSO
    Dim Data
    Dim arr, tmp, output
    Dim file, fileName As String
    Dim x, y As Integer
    Dim newSht As Worksheet
    
    
    Application.ScreenUpdating = False

    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    With fDialog
        .AllowMultiSelect = True
        .Title = "Please select files to import"
        .Filters.Clear
        .Filters.Add "VBO Files", "*.vbo" 'VBO Files are opened and handled like Text Files
        
        If .Show = True Then
            For Each fPath In .SelectedItems
                Set FSO = CreateObject("Scripting.FilesystemObject")
                fileName = FSO.GetFilename(fPath)
                Set Data = FSO.OpentextFile(fPath)
                file = Data.readall
                Data.Close
                
                
                arr = Split(file, vbCrLf)
                ReDim output(UBound(arr), 50)
                For x = 0 To UBound(arr)
                    tmp = Split(arr(x), " ")
                    For y = 0 To UBound(tmp)
                        output(x, y) = tmp(y)
                    Next
                Next
                
                Set newSht = ActiveWorkbook.Sheets.Add(after:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count))
                newSht.Name = fileName
                
                Sheets(fileName).Range("A1").Resize(UBound(output) + 1, UBound(output, 2)) = output
            Next
        End If
    End With
    
    Application.ScreenUpdating = True
    
End Sub

The record of the processed file is not separated rudimentarily correctly and reasonably. I have also already tried using

With Application
     .DecimalSeparator = "."
     .ThousandsSeparator = ","
     .UseSystemSeparator = False
End With

but that did not work either. Or rather, it changed the separators, but the result stayed the same. The numbers were not separated at the correct places. I found a similar question here ( Importing CSV US formatted numbers in Excel with localisation ), which seems to be the same problem. But since the import function in the answer is different from mine, I am not sure how to integrate it properly.

Does someone have a idea? Maybe a way how to preserve the format while or during splitting? Or a better place to integrate the Application.DecimalSeparator argument in the given code?

Thanks for the help!

You may be able to obtain your desired output using Power Query , available in Windows Excel 2010+ and Office 365 Excel

  • Data => Get &Transform => From Text/Csv
  • When the initial dialog opens, ensure Space is selected as the Delimiter
  • Select Transform Data
  • Select Home=>Advanced Editor
  • Replace the `#"Changed Type" line with the code below
    • As written, the code assumes all columns are "numbers" with dot for thousands separator and comma for decimal.
    • If that is not the case, you may need to make some changes
let
    Source = Csv.Document(File.Contents("C:\Users\ron\Desktop\decimals.txt"),
        [Delimiter=" ", Columns=14, Encoding=1252, QuoteStyle=QuoteStyle.None]),
    
//Replace this line into your code
    #"Changed Type" = Table.TransformColumnTypes(Source, 
        List.Transform(Table.ColumnNames(Source), each {_, type number}),"da-DK")
in
    #"Changed Type"

Original Data

| 009   | 090308,510    | +2475,77145123    | -0091,51682637    | 070,530   | 271.89    | +00168,67     | +001,130  | 0,000000  | -8.485680E-04     | +4.625850E-04     | +2.679440E+36     | -2.544081E-29     | +2.658468E+36     |

在此处输入图像描述

Results
在此处输入图像描述

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