Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading text file 1

Status
Not open for further replies.

axism

MIS
May 17, 2005
58
Hi, I have a text file with the following information:

name
email
department
phone number

on each line. I would like to know how you read the text file using vba code line by line. Thanks
 
You can use the Scripting.FilesystemObject in VB. Go to Tools-Reference and select Microsoft Scripting runtime. Create an object for this. This object has methods and properties to read/write files.
 
Using built-in VBA commands

Code:
Dim f As Long
Dim fn As String
Dim txt As String

fn = "c:\MyTextFile.Txt"
f = FreeFile
Open fn For Input Access Read As f
Do Until EOF(f)
  Line Input #f, txt
  'Do something with txt
Loop
Close f

Hope this helps.


 
thanks for answering. it works out great. now that i can read my file. The only question left is that how many files can u open at a time for writing? before, i was only writing to 1 file and reading off that 1 file. i am trying to write to 2 files. it says my file already open. it's it limited to only openning 1 file at a time?
 
Have a look at the FreeFile function.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top