#1   Report Post  
Posted to microsoft.public.excel.misc
kkjensen
 
Posts: n/a
Default Extract from a file


The Excel VBA helpfile has the following code for extracting a character
from the beginning of a textfile. If I change the parameters to
inputs(5,#1) it will extract the first 5 characters of the file.


Code:
--------------------

Dim MyChar
Open "TESTFILE" For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
MyChar = Input(1, #1) ' Get one character.
Debug.Print MyChar ' Print to the Immediate window.
Loop
Close #1 ' Close file.



--------------------


I have a very big textfile and don't want to start my string from the
beginning. Is there a way to get the INPUT function (or another) to
read data out of the middle of a text file? Lets say I want just line
3.

Another problem: I would like to get just one line...how do I detect
the end of the line?


--
kkjensen
------------------------------------------------------------------------
kkjensen's Profile: http://www.excelforum.com/member.php...o&userid=35911
View this thread: http://www.excelforum.com/showthread...hreadid=557043

  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Extract from a file

Instead of getting one character at a time, you can retrieve a whole line:

Option Explicit
Sub testme01()

Dim myLine As String
Dim myFileName As String
Dim FileNum As Long
Dim lCtr As Long
Dim KeepThisLine As String

myFileName = "C:\my documents\excel\test.txt"

FileNum = FreeFile

If Dir(myFileName) = "" Then 'not found
MsgBox "File is missing!"
Exit Sub
Else
Close FileNum
Open myFileName For Input As FileNum
lCtr = 0
KeepThisLine = ""
Do While Not EOF(FileNum)
Line Input #FileNum, myLine
lCtr = lCtr + 1
If lCtr = 3 Then
KeepThisLine = myLine
Exit Do
End If
Loop
Close FileNum

If lCtr < 3 Then
MsgBox "not enough lines"
Else
MsgBox KeepThisLine
End If
End If

End Sub

kkjensen wrote:

The Excel VBA helpfile has the following code for extracting a character
from the beginning of a textfile. If I change the parameters to
inputs(5,#1) it will extract the first 5 characters of the file.

Code:
--------------------

Dim MyChar
Open "TESTFILE" For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
MyChar = Input(1, #1) ' Get one character.
Debug.Print MyChar ' Print to the Immediate window.
Loop
Close #1 ' Close file.



--------------------

I have a very big textfile and don't want to start my string from the
beginning. Is there a way to get the INPUT function (or another) to
read data out of the middle of a text file? Lets say I want just line
3.

Another problem: I would like to get just one line...how do I detect
the end of the line?

--
kkjensen
------------------------------------------------------------------------
kkjensen's Profile: http://www.excelforum.com/member.php...o&userid=35911
View this thread: http://www.excelforum.com/showthread...hreadid=557043


--

Dave Peterson
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
How do I extract an Excel Chart as a graphics file? Dick Hanneman Excel Worksheet Functions 7 May 6th 06 03:05 AM
How to extract value from a different file? Firenzeitl Excel Worksheet Functions 1 April 22nd 06 07:16 AM
Excel file automatically opens Lost4Now Excel Discussion (Misc queries) 6 December 4th 05 09:35 PM
Links picking up values from an older version of linked file Cate Links and Linking in Excel 4 October 20th 05 01:53 PM
cannot open excel file, please help!!! sunlite Excel Discussion (Misc queries) 0 September 5th 05 05:29 PM


All times are GMT +1. The time now is 11:52 PM.

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

About Us

"It's about Microsoft Excel"