Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Start reading text file at line 3

I have the following code and it works as is but I would like the code to
start reading at line three of the text file. Is that possible and if so, how?

Public Sub GetIt()
Set fs = CreateObject("Scripting.FileSystemObject")

Dim r As Integer

Set ImpRng = ActiveCell

strFile = Application.GetOpenFilename
strFile = fs.GetFileName(strFile)

Open strFile For Input As #1
r = 0

Do While Not EOF(1) 'Want to start at line three of text file
Line Input #1, data
ActiveCell.Offset(r, 0) = LTrim(data)
r = r + 1
Loop
Close #1

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Start reading text file at line 3

If you text is managable (<~ 5MB), you get the whole file in one go. Also
the FSO is slow and provides no functionality the VBA does not natively
have.

Dim AllFile() As String
Dim FileNum As Long
Dim i As Long

FileNum = FreeFile

Open ThisWorkbook.Path & "\Book2.csv" For Input As #FileNum
AllFile = Split(Input(LOF(FileNum), FileNum), vbNewLine)
Close #FileNum

For i = LBound(AllFile) + 2 To UBound(AllFile)
'Do something
Debug.Print AllFile(i)
Next

NickHK

"Billy B" wrote in message
...
I have the following code and it works as is but I would like the code to
start reading at line three of the text file. Is that possible and if so,

how?

Public Sub GetIt()
Set fs = CreateObject("Scripting.FileSystemObject")

Dim r As Integer

Set ImpRng = ActiveCell

strFile = Application.GetOpenFilename
strFile = fs.GetFileName(strFile)

Open strFile For Input As #1
r = 0

Do While Not EOF(1) 'Want to start at line three of text file
Line Input #1, data
ActiveCell.Offset(r, 0) = LTrim(data)
r = r + 1
Loop
Close #1

End Sub



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default Start reading text file at line 3

Hi Billy
The SkipLine method from the fileSystemObject is one way:
strFile = Application.GetOpenFilename
strFileToRead = fs.OpenTextFile(strFile,ForReading)
For i = 1 to 2
strFileToRead.SkipLine
Next i
strFileToRead.ReadLine

....

HTH
Cordially
Pascal


"Billy B" a écrit dans le message de
news: ...
I have the following code and it works as is but I would like the code to
start reading at line three of the text file. Is that possible and if so,
how?

Public Sub GetIt()
Set fs = CreateObject("Scripting.FileSystemObject")

Dim r As Integer

Set ImpRng = ActiveCell

strFile = Application.GetOpenFilename
strFile = fs.GetFileName(strFile)

Open strFile For Input As #1
r = 0

Do While Not EOF(1) 'Want to start at line three of text file
Line Input #1, data
ActiveCell.Offset(r, 0) = LTrim(data)
r = r + 1
Loop
Close #1

End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a text file line by line stressman Excel Programming 3 October 16th 05 05:29 AM
Reading a text file line by line Foss[_2_] Excel Programming 4 March 16th 05 04:01 PM
Trying to start a second line ( ie use of enter key) in an output file Susan Hayes Excel Programming 1 February 26th 05 09:20 PM
Excel 2000 Hanging while reading large file with Line Input Jacques Brun Excel Programming 4 February 21st 04 05:05 PM


All times are GMT +1. The time now is 08:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"