KingKong
KingKong

Reputation: 421

Parsing txt file into Excel with multiple delimiters

Is there a way to parse lines of data from a .txt file using multiple delimitters? Specifically, I'm importing a .txt file using VBA and QueryTables and the .TextFileOtherDelimiter method. But my file has different delimitters (ex: ":" & "=") that I want to be able separate in one passing.

Also, is there a way to parse the data using more than one character such as ".-" or ":(tab)"?

I'm iterating through a bunch of files in multiple directories. Here's the code I'm using:

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

So can my TestDataDelimiter be something besides a single char?

Upvotes: 0

Views: 2906

Answers (1)

Gary's Student
Gary's Student

Reputation: 96753

Read the file in line-by-line. Then convert the multiple delimiters into a single delimiter by using Replace(). Then use Split() on the common delimiter.

EDIT#1:

Here is some sample code to illustrate. Say we want to parse using both + and -

with data like:

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

This macro:

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

will produce:

enter image description here

Upvotes: 1

Related Questions