count number of lines in a file

Posted on 2003-03-20
Medium Priority
Last Modified: 2010-05-01
hi everyone, is it possible to count the number of lines in a file?
I'm using:

    Dim strTemp As String * 1
    Dim i As Integer
    i = 0
    Open App.Path & "\aras.pol" For Input As 1#
    Do Until EOF(1)
    i% = i% + 1
    Line Input #1, strTemp
    Close #1

msgbox "number of lines: " & i

this works but is there another way?
Question by:igfp
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
  • 3
  • 2

Author Comment

ID: 8176496
oh, and also to go to a specific line. I'm using the same thing but when i= "number of the line I want" then it exits the cycle. Is there another way to do this as well?
LVL 13

Accepted Solution

stefri earned 80 total points
ID: 8176710
You can create a file system object
Open the file
Read all lines in a string
use split to cut the string at every vbcr or vblf or vbcrlf
the number of lines will be the upperbound of the array -1
Going to line N would be array(N-1)

This code has been tested in Excel VBA

Option Explicit

Function numLines (theFile) As Long

  Const ForReading = 1, ForWriting = 2
  Dim fso As Object, f As Object
  Dim allLines As String
  Dim linesSplit() As String

  Set fso = CreateObject("Scripting.FileSystemObject")

  Set f = fso.OpenTextFile(theFile, ForReading)
  allLines = f.ReadAll

  linesSplit = Split(allLines, vbCrLf)
  numLines = UBound(linesSplit) - 1
  Set f = Nothing
  Set fso = Nothing
End Function

Sub test()
Dim n As Long

n = numLines ("c:\setuplog.txt")

End Sub

LVL 22

Expert Comment

ID: 8177097
Similar to stefri and yours, you can read the entire file in one shot, then "count" the number of newline characters.

dim strTemp as strnig
dim strLinesSplit() as string
dim iNumLines as integer

' read the entire file
Open App.Path & "\aras.pol" For Input As 1#
strTemp = input$(lof(1), #1)
Close #1

' count the number of newlines, then add one for the last line
strLinesSplit = Split(strTemp, vbNewLine)
iNumLines = UBound(linesSplit) - 1

The key here is the Input$ and the Split functions.
Independent Software Vendors: 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: 8184393
my question now is, since i like mine the best, wich one takes the points????
LVL 22

Expert Comment

ID: 8184425
>is there another way
Since this Q was answered in the first comment (stefri), I think you should accept that as your answer.

Author Comment

ID: 8184462
as you wish! ;) Thank you both!

Featured Post

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!

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 8 hours left to enroll

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