【问题标题】:Parsing txt file into Excel with multiple delimiters使用多个分隔符将文本文件解析为 Excel
【发布时间】:2015-08-15 21:37:04
【问题描述】:

有没有办法使用多个分隔符解析 .txt 文件中的数据行?具体来说,我正在使用 VBA 和 QueryTables 以及 .TextFileOtherDelimiter 方法导入一个 .txt 文件。但是我的文件有不同的分隔符(例如:“:”和“=”),我希望能够一次性分开。

另外,有没有一种方法可以使用多个字符(例如“.-”或“:(tab)”)来解析数据?

我正在遍历多个目录中的一堆文件。这是我正在使用的代码:

For Each File In Folder.Files
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" _
          & File, Destination:=Range("A1"))
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = DataStartRow
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = True
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = TestDataDelimiter
            .TextFileColumnDataTypes = Array(1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
next

那么我的 TestDataDelimiter 可以是单个字符以外的东西吗?

【问题讨论】:

    标签: excel vba parsing


    【解决方案1】:

    逐行读取文件。然后使用 Replace() 将多个分隔符转换为单个分隔符。然后在公共分隔符上使用 Split()。

    编辑#1:

    这里有一些示例代码来说明。假设我们要同时使用 + 和 -

    进行解析

    数据如下:

    hello+world-goodby+for-now
    12+34+qwerty-poiunyt
    

    这个宏:

    Sub ParseData()
        Dim FileSpec As String, TextLine As String
        Dim RowNumber As Long
        folder = "C:\TestFolder\textfiles"
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set folder = fso.GetFolder(folder)
        RowNumber = 1
        Close #1
    
        For Each file In folder.Files
            FileSpec = folder & "\" & file.Name
            Open FileSpec For Input As #1
            Do While Not EOF(1)
                Line Input #1, TextLine
                TextLine = Replace(TextLine, "+", "-")
                If InStr(TextLine, "-") = 0 Then
                    Cells(RowNumber, 1) = TextLine
                Else
                    ary = Split(TextLine, "-")
                    ccol = 1
                    For Each a In ary
                        Cells(RowNumber, ccol) = a
                        ccol = ccol + 1
                    Next a
                End If
                RowNumber = RowNumber + 1
            Loop
            Close #1
        Next file
    End Sub
    

    将产生:

    【讨论】:

    • 我将代码添加到问题中。这将如何与我所拥有的一起工作?我不认为我可以访问每一行,所以我不确定我将如何操作它的那部分。
    • @KingKong 查看我的EDIT#1
    • 哦,我不知道其中一些功能。感谢您向我展示了一些新的 VBA 功能。这似乎确实有效。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    相关资源
    最近更新 更多