PDA

View Full Version : Count number of lines



jmentor
01-31-2006, 06:26 AM
Is there a way of counting the number of lines
in a text file and then to abort a process if number
of lines exceed 1.
Such as
If CountLines(strFileName) > 1 then
MsgBox "Text"

Exit of abort


I already have a variable declared for the filename

Any help greatly appreciated

matthewspatrick
01-31-2006, 07:35 AM
This function will return the number of lines. I do not necessarily recommend it for really large text files :rotlaugh:



Function CountLines(FileNameAndPath As String)

Dim fso As Object
Dim ts As Object
Dim arr As Variant

Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(FileNameAndPath)

arr = Split(ts.ReadAll, Chr(10))

ts.Close
Set ts = Nothing
Set fso = Nothing

CountLines = UBound(arr) + 1

End Function


Patrick