ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   EOF Parse Text file (https://www.excelbanter.com/excel-programming/417525-eof-parse-text-file.html)

Bam

EOF Parse Text file
 
I have a text file which I need to retrieve the last 6 characters from.

The file Is always 2 Lines and looks like this.

RESULT OK
INFORMATION Add Job 185792


I want to retrieve the (Job) Number at the end - "185792".

My current script looks like this:
(Where strTarget is the textfile.)

Open strTarget For Input As #1
Do While Not EOF(1)
Line Input #1, data
Debug.Print Right(data, 6)
Loop
Close #1

The result however comes out like this:

ULT OK
185792

How Do I Only retrieve the 2nd line of this?

And even better put it into a particular Cell.

Eg: MyRow = ActiveCell.Row
Destination:=Worksheets("Sheet 1").Cells(Myrow, 6)

Thanks?

Bam.

Rick Rothstein

EOF Parse Text file
 
The reason you are getting that output is because your print statement is
inside the loop meaning you are printing out the last six characters of
**each** line you read. If you move the print statement outside the loop,
then each iteration of the loop will assign the line that it just read in to
the data variable, which means the previously assigned value is overwritten.
The net effect of this is that when the loop finishes, the data variable
will contain the last line of the file. Hence, if you wait to print out the
last six characters in the data variable until after the loop has finished,
then you will only print out the last six characters of the last line in the
file...

Open strTarget For Input As #1
Do While Not EOF(1)
Line Input #1, data
Loop
Debug.Print Right(data, 6)
Close #1

--
Rick (MVP - Excel)


"Bam" wrote in message
...
I have a text file which I need to retrieve the last 6 characters from.

The file Is always 2 Lines and looks like this.

RESULT OK
INFORMATION Add Job 185792


I want to retrieve the (Job) Number at the end - "185792".

My current script looks like this:
(Where strTarget is the textfile.)

Open strTarget For Input As #1
Do While Not EOF(1)
Line Input #1, data
Debug.Print Right(data, 6)
Loop
Close #1

The result however comes out like this:

ULT OK
185792

How Do I Only retrieve the 2nd line of this?

And even better put it into a particular Cell.

Eg: MyRow = ActiveCell.Row
Destination:=Worksheets("Sheet 1").Cells(Myrow, 6)

Thanks?

Bam.



Bam

EOF Parse Text file
 
Thanks Rick - I actually just figured that out also.


Open strTarget For Input As #1
Do While Not EOF(1)
Line Input #1, data
Loop
strJob = Right(data, 6)
Close #1
Cells(MyRow, 6).Value = strJob


"Rick Rothstein" wrote:

The reason you are getting that output is because your print statement is
inside the loop meaning you are printing out the last six characters of
**each** line you read. If you move the print statement outside the loop,
then each iteration of the loop will assign the line that it just read in to
the data variable, which means the previously assigned value is overwritten.
The net effect of this is that when the loop finishes, the data variable
will contain the last line of the file. Hence, if you wait to print out the
last six characters in the data variable until after the loop has finished,
then you will only print out the last six characters of the last line in the
file...

Open strTarget For Input As #1
Do While Not EOF(1)
Line Input #1, data
Loop
Debug.Print Right(data, 6)
Close #1

--
Rick (MVP - Excel)


"Bam" wrote in message
...
I have a text file which I need to retrieve the last 6 characters from.

The file Is always 2 Lines and looks like this.

RESULT OK
INFORMATION Add Job 185792


I want to retrieve the (Job) Number at the end - "185792".

My current script looks like this:
(Where strTarget is the textfile.)

Open strTarget For Input As #1
Do While Not EOF(1)
Line Input #1, data
Debug.Print Right(data, 6)
Loop
Close #1

The result however comes out like this:

ULT OK
185792

How Do I Only retrieve the 2nd line of this?

And even better put it into a particular Cell.

Eg: MyRow = ActiveCell.Row
Destination:=Worksheets("Sheet 1").Cells(Myrow, 6)

Thanks?

Bam.





All times are GMT +1. The time now is 08:58 AM.

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