View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default number of lines and characters in a text file

There's LOF for characters (or FileLen if closed) but I don't know a direct
way to get the line count. Unless the file is very large I wouldn't think
using EOF would take so long but have a go with this -

Sub test()
Dim cntLen As Long, cntChars As Long, cntLines As Long
Dim sFile As String
Dim FF As Integer

sFile = "C:\temp\test.txt"
cntLen = FileLen(sFile)

FF = FreeFile
Open sFile For Input As #FF
Seek FF, 1
cntChars = LOF(FF)
cntLines = UBound(Split(Input(cntChars, FF), vbCrLf)) + 1
Close #FF

' cntChars will include returns
Debug.Print cntLines, cntChars, cntLen
End Sub

Regards,
Peter T


"Bob Flanagan" wrote in message
...
Is there a way to get the number of lines and characters in a text file
without opening it in Excel or via reading each line until EOF is reached?

Bob