STart reading text file at line 2

S

Striker

Using the following code to open a text file and extract data. The problem
is the first line of the text file contains junk, it contains one long
number that is a combination of the date and number of records in the file.
This could be up to 1700 records.

So my question is, is there a way to open the file and move to the second
line to begin getting data?

Earlier in this sub I am setting a counter (iInt) so I can let the user know
how many records are imported. I was thinking I could easily add a IF
statement in there. IF the counter is less than 1 do something, that way
the first line it won't import anything. Problem with that statement is it
will only test TRUE one time. Seems like a inefficient way to do it. Add
IF that will test true only the first time, and false the next 1699 times.
Would be much better to simply start reading the file at line 2.

Any Ideas?


Than You

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'Open text data file
Open "C:\Test.txt" For Input As #1

Worksheets("Import").Activate

'Extract the data I need from the file.
Do Until EOF(1)
Line Input #1, sLineOfText
rRange.Offset(0, 0) = Trim(Mid(sLineOfText, 63, 35)) 'FirstName
rRange.Offset(0, 1) = Trim(Mid(sLineOfText, 98, 20)) 'LastName
rRange.Offset(0, 2) = Trim(Mid(sLineOfText, 179, 30)) 'Address
rRange.Offset(0, 3) = Trim(Mid(sLineOfText, 209, 20)) 'City
rRange.Offset(0, 4) = Trim(Mid(sLineOfText, 229, 2)) 'State
rRange.Offset(0, 5) = Trim(Mid(sLineOfText, 231, 5)) 'Zip
rRange.Offset(0, 6) = Trim(Mid(sLineOfText, 11, 7)) 'Reference #

'move down one row
Set rRange = rRange.Offset(1, 0)

'increment iInt
iInt = iInt + 1
Loop
Close #1
 
A

Ardus Petus

'Open text data file
Open "C:\Test.txt" For Input As #1

Worksheets("Import").Activate

'Skip First line
Line input #1, sLineOfText

'Extract the data I need from the file. Do Until EOF(1)
Line Input #1, sLineOfText
rRange.Offset(0, 0) = Trim(Mid(sLineOfText, 63, 35)) 'FirstName
rRange.Offset(0, 1) = Trim(Mid(sLineOfText, 98, 20)) 'LastName
rRange.Offset(0, 2) = Trim(Mid(sLineOfText, 179, 30)) 'Address
rRange.Offset(0, 3) = Trim(Mid(sLineOfText, 209, 20)) 'City
rRange.Offset(0, 4) = Trim(Mid(sLineOfText, 229, 2)) 'State
rRange.Offset(0, 5) = Trim(Mid(sLineOfText, 231, 5)) 'Zip
rRange.Offset(0, 6) = Trim(Mid(sLineOfText, 11, 7)) 'Reference #

'move down one row
Set rRange = rRange.Offset(1, 0)

'increment iInt
iInt = iInt + 1
Loop
Close #1
 
D

Dave Peterson

You could add another "line input" directly above the "do until" line.

But I'd just delete that first row after I imported the whole file.

Just keep track of where rRange is first used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top