Solved

Posted on 2003-03-12
Medium Priority
133 Views
Is there a Function that return the number of line in a file or i have to make it make it myself?
0
Question by:Dayus
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

Author Comment

ID: 8123112
a txt File !
0

LVL 1

Accepted Solution

hj2k3 earned 80 total points
ID: 8123370
i'm not aware of a function that will do this for you, but you could write one fairly simply:

dim fileText as string
dim i as long
dim fLen as long
dim nLines as long

open "whatever.txt" for input as #1
input #1, fileText
close #1
'the whole file is now in fileText

fLen = len(filetext)
i = inStr(1, fileText, vbcrlf)   'find the first end of line

do until i = 0
nLines = nLines + 1
i = inStr(i+1, fileText, vbcrlf)
loop

now nLines contains the number of lines in the file

hope this helps
0

LVL 11

Expert Comment

ID: 8123374
You could use FileLen function to get the size of the file in bytes.  but if you want number of lines, then write your own function similar to ....

Private Function NumOfLines(FileName as String) as Long
Dim strLine as String

NumOfLines = 0
If (Dir(FileName) = "") then Exit Sub

Open FileName For Input as #1
Do
Line Input #1, strLine
NumOfLines = NumOfLines + 1
Loop While (Not EOF(1))
Close #1

End Function

Good Luck!
0

Author Comment

ID: 8123386
both great answer the first get the point...
0

LVL 1

Expert Comment

ID: 8123412
oooooo... that was pretty close! credit to supunr, though, neat answer.
0

Expert Comment

ID: 8126315
Open inputfile For Input As #2
While Not EOF(2)
Line Input #2, linebuffer
linecount = linecount + 1
Wend
Close #2
Linecount contains number of lines.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month9 days, 9 hours left to enroll