VBA reading in text file and parsing by line breaks (carriage returns)

kkmoslephour

New Member
Joined
May 8, 2014
Messages
27
Hi guys, I am a little stuck here. I am trying to read in a text file and parse it by line breaks however I cannot read the content of the parsed line breaks, it just shows up as 'Empty' when I watch them.


Code:
Option Explicit

Sub ParseText()

Dim myFile As String, text As String, textline As String, Lastrow As Integer, i As Integer, Dim data() As String

myFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")


If myFile <> "False" Then
    MsgBox "Opening " & myFile
Else
    MsgBox "Invalid File Path!", vbCritical, vbNullString
    Exit Sub
End If

Open myFile For Input As #1

Do While Not EOF(1)
    Line Input #1, textline
    data = Split(textline, vbCrLf)
    text = text & textline
    MsgBox textline
Loop


However when I parse by space " " using
Code:
[COLOR=#000000][FONT=Arial][B]data = Split(textline, " ")[/B][/FONT][/COLOR]
I can sucessfully see the contents of each array in my console window. So i am wondering why I cannot parse by line breaks.
 
Last edited:
Thanks John_W! I tried everything: vbNewLine, vbCrLf, vbCr...
I did not try vbLf!
That was the one :)
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hello I get subscript out of Range Error using the above code what i've changed here
myFile = "C\ABC\Text.txt"
and i get error at MsgBox data(0) ie after reading Each line of text file. but in End the Line is Blank and after the blank line there is vbcrlf
Any Guidance from anyone shall appreciate
VBA Code:
Option Explicit

Sub ParseText()

Dim myFile As String, text As String, textline As String, Lastrow As Integer, i As Integer, Dim data() As String

''''''''''''myFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
myFile = "C\ABC\Text.txt"

If myFile <> "False" Then
    MsgBox "Opening " & myFile
Else
    MsgBox "Invalid File Path!", vbCritical, vbNullString
    Exit Sub
End If

Open myFile For Input As #1

Do While Not EOF(1)
    Line Input #1, textline
    data = Split(textline, vbLf)
    '''''''''text = text & textline
    '''''''''MsgBox textline
MsgBox data(0)
Loop
Close #1
End Sub
SamD
160
 
Upvote 0
In this line of code...

myFile = "C\ABC\Text.txt"

Your drive letter (the first "C") needs a colon after it....

myFile = "C:\ABC\Text.txt"

Also, since you are specifying your file/path name directly instead of picking it from the "Open File" dialog box, you can remove this part of the code which is there just to show you the file you picked from the dialog box or exit the program if you cancelled out of the dialog box without selecting any file...
VBA Code:
If myFile <> "False" Then
    MsgBox "Opening " & myFile
Else
    MsgBox "Invalid File Path!", vbCritical, vbNullString
    Exit Sub
End If
 
Last edited:
Upvote 0
Rick
Path without colon would give another error. Its Typo Error when posting #12. Sorry. Anyways thanks for the correction
As per your suggestion removed the part of the code
VBA Code:
If myFile <> "False" Then
    MsgBox "Opening " & myFile
Else
    MsgBox "Invalid File Path!", vbCritical, vbNullString
    Exit Sub
End If
After removing the above part of the code Still getting Subscript Out of Range Error at MsgBox data(0) ie after Each line of text file. but in End the Line is Blank and after the blank line there is vbcrlf. What i observed here is that after reading each line of text file and at the end of line there seems to be empty line and after empty line there is vbcrlf. Below structure of myFile = "C:\ABC\Text.txt"
19-09-2020
Reference Number NZ26X201249Y75140
Amt 2000.00

28-09-2020
Reference Number MD00X1485Y47366
Amt 8600.00

10-10-2020
Reference Number YN27220X1562M9109
Amt 7650.00

SamD
161
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top