简体   繁体   English

位置导致文件导入期间 Excel 中的分隔符放置不正确

[英]Location causes incorrect placement of separators in Excel during file import

I import large amounts of data into Excel.我将大量数据导入 Excel。 These are previously reduced in quantity from 100 Hz to 1 Hz by a third-party program to reduce work and load time.这些先前由第三方程序从 100 Hz 减少到 1 Hz,以减少工作和负载时间。 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.我在这里发现了一个类似的问题( Importing CSV US formatted numbers in Excel with localization ),这似乎是同样的问题。 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?还是在给定代码中集成 Application.DecimalSeparator 参数的更好地方?

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您可以使用 Windows Excel 2010+ 和 Office 365 Excel 中提供的Power Query获得所需的输出

  • Data => Get &Transform => From Text/Csv
  • When the initial dialog opens, ensure Space is selected as the Delimiter当初始对话框打开时,确保选择Space作为分隔符
  • Select Transform Data选择Transform Data
  • Select Home=>Advanced Editor选择Home=>Advanced Editor
  • Replace the `#"Changed Type" line with the code below用下面的代码替换 `#"Changed Type" 行
    • 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结果
在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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