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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number of rows in a text file

Status
Not open for further replies.

CGantz

Programmer
May 14, 2001
17
US
Hello,

I'm trying to retrieve the number of rows that are in a text file via vba. I'm transferring data from one system to another in a series of files and I want to record the number of records I'm transfering in a spreadsheet. Is there an easy way to do this? My first thought is to open the file with vba code and store the number of Rows in a variable but I'm fuzzy on the code for getting the number of rows.

Thanks,

Cole
 
Try this code:

Function TestImport()

Dim xferData As String
Dim xferFile As String
xferFile = "C:\Test.txt"
xferData = 0
Close #1
Open xferFile For Input As #1 ' Open file for input.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, xferData ' Read file data into variable.
xferData = xferData + 1
Loop

MsgBox "The number of records in Test.txt were :" & xferData

End Function
Anthony J. DeSalvo
President - ScottTech Software
"Integrating Technology with Business"
 
ajdesalvo's code will work, but it requires one more variable, e.g. strDummy, and the line input command should read strDummy, not xferData.
Rob
[flowerface]
 
Thanks Rob! [thumbsup2] Anthony J. DeSalvo
President - ScottTech Software
"Integrating Technology with Business"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top