Thatdude1
Thatdude1

Reputation: 903

Concatenating lines from text file - Excel VBA

I have a text file in a format like so,

Text:

-- Begin 
  Line1
  Line2
  Line3
      ^
-- Begin
  Line1
  Line2
  Line3
  Line4
      ^
.
.
.
.

I basically want to put Line1 to Line(whatever) between the lines --Begin to ^ in an array so each element in the array is bunch of lines Example of array

Array =  [("Line1"  & vbNewLine & "Line 2") , ("Line1"  & vbNewLine & "Line 2" & vbNewLine & "Line 3") ... ]

But basically want to store each element in the array in a cell. (Might not even need to use an array) ...

Not sure if this is even possible in excel VBA, but this is what I've tried so far

    Dim FileNum As Integer
    Dim DataLine As String
    Dim Lines As Variant
    Dim j As Integer


    FileNum = FreeFile()
    Open "C:..." For Input As #FileNum


 While Not EOF(FileNum)
       Line Input #FileNum, DataLine

      If InStr(DataLine, "-- Begin") > 0 Then
        nextLinecounter = 1

      ElseIf InStr(DataLine, "^") > 0 Then
        nextLinecounter = 0
        j = j + 1

      ElseIf nextLinecounter = 1 Then
       Lines(j) = DataLine + .. Somehow concatenate next lines into array

     End If


Wend

I'm stuck how to skip next line and append it to the current entry, any way of doing this thanks.

Upvotes: 1

Views: 2295

Answers (1)

user2140173
user2140173

Reputation:

So I would do it a bit differently. Using a more up to date approach for file reading.

See more details on how to read *.txt files in VBA - here

Note: you need to add references to Microsoft Scripting Runtime via VBE -> Tools -> References

Option Explicit

Sub ReadTxtFile()

    Dim oFSO As New FileSystemObject
    Dim oFS As TextStream

    Dim filePath As String
    filePath = "C:\Users\" & Environ$("username") & "\Desktop\foo.txt"

    If Not fileExist(filePath) Then GoTo FileDoesntExist

    On Error GoTo Err

    ReDim arr(0) As String
    Dim s As String

    Set oFS = oFSO.OpenTextFile(filePath)
    Do While Not oFS.AtEndOfStream
        Dim line As String
        line = oFS.ReadLine
        If InStr(line, "-- Begin") = 0 And InStr(line, "^") = 0 Then
            s = s & line
        End If
        If InStr(line, "^") > 0 Then
            arr(UBound(arr)) = s
            ReDim Preserve arr(UBound(arr) + 1)
            s = vbNullString
        End If
    Loop
    ReDim Preserve arr(UBound(arr) - 1)
    oFS.Close

    Dim k As Long
    For k = LBound(arr) To UBound(arr)
        Debug.Print k, arr(k)
    Next k
    Exit Sub

FileDoesntExist:
    MsgBox "File Doesn't Exist", vbCritical, "File not found!"
    Exit Sub

Err:
    MsgBox "Error while reading the file.", vbCritical, vbNullString
    oFS.Close
    Exit Sub

End Sub


Function fileExist(path As String) As Boolean
    fileExist = IIf(Dir(path) <> vbNullString, True, False)
End Function

the foo.txt looks like this

-- Begin 
  Line1
  Line2
  Line3
      ^
-- Begin
  Line1
  Line2
  Line3
  Line4
      ^

your array looks like this

enter image description here

Upvotes: 1

Related Questions