ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Reading a text file line by line (https://www.excelbanter.com/excel-programming/342926-reading-text-file-line-line.html)

stressman

Reading a text file line by line
 

I am looking to bring data into a excel file as a string one line at a
time so I can evaluate the type of data on the line. When I have found
the right data I can brake the string up into the right cells. The line
might have the load case, data type or loads. How do I open the text
file with VBA in Excel and read the file line by line into a string?


--
stressman
------------------------------------------------------------------------
stressman's Profile: http://www.excelforum.com/member.php...o&userid=28139
View this thread: http://www.excelforum.com/showthread...hreadid=476545


Chip Pearson

Reading a text file line by line
 
Try something like the following:

Dim S As String
Open "H:\test.txt" For Input As #1 ' change filename
Do Until EOF(1)
Line Input #1, S
Debug.Print S
Loop
Close #1


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"stressman"
wrote in
message
...

I am looking to bring data into a excel file as a string one
line at a
time so I can evaluate the type of data on the line. When I
have found
the right data I can brake the string up into the right cells.
The line
might have the load case, data type or loads. How do I open
the text
file with VBA in Excel and read the file line by line into a
string?


--
stressman
------------------------------------------------------------------------
stressman's Profile:
http://www.excelforum.com/member.php...o&userid=28139
View this thread:
http://www.excelforum.com/showthread...hreadid=476545




Gary Keramidas

Reading a text file line by line
 
i have used this and then modified it to only bring in the line i needed,
but this brings in every line of every .eml file in the folder
it will put it in consecutive lines in column A

Dim ColNdx As Integer
Dim WholeLine As String
Dim FileDir As Variant
Dim FilesInPath As String
Dim MyFiles() As String
Dim NumberOfFiles As Long
Sub import()
Sheets("sheet1").Range("A1").Select
ColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
sRow = RowNdx
FileDir = Environ("USERPROFILE") + "\Desktop\Cats\" ' change this
' determine # of files
FilesInPath = Dir(FileDir & "\*.eml") ' change this location
NumberOfFiles = 0
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

' perform import of email files
Do While FilesInPath < ""
Open FileDir & FilesInPath For Input Access Read As #1
While Not EOF(1)
Line Input #1, WholeLine
Cells(RowNdx, ColNdx).Value = WholeLine
RowNdx = RowNdx + 1
Wend
Close #1
' after import, delete unwanted rows
' Range(Rows(sRow), Rows(sRow + 23)).Delete
' Range("A" & sRow).Offset(1, 0).Select
' sRow = sRow + 1
' RowNdx = RowNdx +1
NumberOfFiles = NumberOfFiles + 1

ReDim Preserve MyFiles(1 To NumberOfFiles)
MyFiles(NumberOfFiles) = FilesInPath
FilesInPath = Dir()
Loop
End Sub
--


Gary


"stressman" wrote
in message ...

I am looking to bring data into a excel file as a string one line at a
time so I can evaluate the type of data on the line. When I have found
the right data I can brake the string up into the right cells. The line
might have the load case, data type or loads. How do I open the text
file with VBA in Excel and read the file line by line into a string?


--
stressman
------------------------------------------------------------------------
stressman's Profile:
http://www.excelforum.com/member.php...o&userid=28139
View this thread: http://www.excelforum.com/showthread...hreadid=476545




stressman[_2_]

Reading a text file line by line
 

Thanks that 'Line Input' command done the trick.
I need to get a new book it did not have that command in it.


--
stressman
------------------------------------------------------------------------
stressman's Profile: http://www.excelforum.com/member.php...o&userid=28139
View this thread: http://www.excelforum.com/showthread...hreadid=476545



All times are GMT +1. The time now is 12:20 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com