繁体   English   中英

VBA将定界符文本文件转换为Excel

[英]VBA Convert delimiter text file to Excel

我有一个代码将文件夹中的文件从.txt(带有“ |”分隔符)转换为xslx,但是对于某些文件(当我在excel中打开文件时),该代码工作正常,而当我尝试导入文件时,其他代码是错误的用excel Ribbon手动拧一个(从文本中获取外部数据->),文件正确。

这是我的代码:

Sub tgr()

    Const txtFldrPath As String = "C:\...\txtFiles"     
    Const xlsFldrPath As String = "C:\excelFiles"     

    Dim CurrentFile As String: CurrentFile = Dir(txtFldrPath & "\" & "*.txt")
    Dim strLine() As String
    Dim LineIndex As Long

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    While CurrentFile <> vbNullString
        LineIndex = 0
        Close #1
        Open txtFldrPath & "\" & CurrentFile For Input As #1
        While Not EOF(1)
            LineIndex = LineIndex + 1
            ReDim Preserve strLine(1 To LineIndex)
            Line Input #1, strLine(LineIndex)
        Wend
        Close #1

        With ActiveSheet.Range("A1").Resize(LineIndex, 1)
            .Value = WorksheetFunction.Transpose(strLine)
            .TextToColumns Other:=True, OtherChar:="|"
        End With

        ActiveSheet.UsedRange.EntireColumn.AutoFit
        ActiveSheet.Copy
        ActiveWorkbook.SaveAs xlsFldrPath & "\" & Replace(CurrentFile, ".txt", ".xlsx"), xlOpenXMLWorkbook
        ActiveWorkbook.Close False
        ActiveSheet.UsedRange.ClearContents

        CurrentFile = Dir
    Wend
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

在此处输入图片说明 此图片显示了excel中的结果和原始文件.txt

首先,您的数据不正确。 AI060616.txt中的总负债30,619,676.00之间有一个制表符。 Excel不喜欢单元格内容中的选项卡。 实际上,制表符是TextToColumns命令上的默认定界符,并且在引入数组时会转换为两列数据。

    Open txtFldrPath & "\" & CurrentFile For Input As #1
    While Not EOF(1)
        LineIndex = LineIndex + 1
        ReDim Preserve strLine(1 To LineIndex)
        Line Input #1, strLine(LineIndex)
        'STRIP TABS OUT AND REPLACE WITH A SPACE!!!!!
        strLine(LineIndex) = Replace(strLine(LineIndex), Chr(9), Chr(32))
    Wend
    Close #1

接下来, Range.TextToColumns方法 “记住”上一次运行时使用的所有设置。 不管是由用户在工作表上还是通过VBA进行。 您需要提供的参数多于提供的参数,以确保它可以按您想要的方式运行。

    With ActiveSheet.Range("A1").Resize(LineIndex, 1)
        .Value = WorksheetFunction.Transpose(strLine)
        'DEFINE THE OPERATION FULLY!!!!
        .TextToColumns Destination:=.Cells(1), DataType:=xlDelimited, _
                       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
                       Tab:=False, Semicolon:=False, Comma:=False, Space:=False, _
                       Other:=True, OtherChar:="|"
    End With

import_TXT

虽然我还不了解解决方案/问题,但是如果您两次应用.TextToColumns方法,似乎可以解决此问题:

With ActiveSheet.Range("A1").Resize(LineIndex, 1)
    .Value = WorksheetFunction.Transpose(strLine)
    .TextToColumns Other:=True, OtherChar:="|"
    .TextToColumns Other:=True, OtherChar:="|"
End With

也许其他人可以阐明原因。 虽然这不是我通常提供的那种帖子,但是上面的内容应该为您提供一种解决方法,而其他人可以提供更好的解决方案(包括说明)。

文件AI150616在“ B”列中的数据为30,619,676.00 Net worth = ZAR 83,456,466.00 Hence, final required BG should be (63,503,915.82)
在此处输入图片说明 分隔符仅适用于一列。 应该允许吗? 如果不正确,则可能需要另一个过程来检查数据是否正确,如果正确,则将其附加,否则,警告用户数据已以某种方式被泄露。

暂无
暂无

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

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