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

虽然我还不明白解决方案/问题,但如果你应用.TextToColumns方法两次似乎就解决了:

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

也许其他人可以解释为什么会这样。虽然这不是我通常提供的那种 post,但上面的内容应该会给你一个解决方法,而其他人会提出更好的解决方案(包括解释)。

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

首先,您的数据有误。 总负债和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 method'remembers'上次使用的所有设置运行通过;不管是用户在 hte 工作表上还是通过 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