Files Reading Question!

Posted on 2003-03-12
Medium Priority
Last Modified: 2011-09-20
Is there a Function that return the number of line in a file or i have to make it make it myself?
Question by:Dayus
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
  • Learn & ask questions

Author Comment

ID: 8123112
a txt File !

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)

now nLines contains the number of lines in the file

hope this helps
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
           Line Input #1, strLine
           NumOfLines = NumOfLines + 1
        Loop While (Not EOF(1))
    Close #1

End Function

Good Luck!
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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

Expert Comment

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

Expert Comment

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

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

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

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question