How to count number of lines in a text file
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 'Text File Handling - How to count number of lines in a text file Public Function CountLineInTxtFile(ByVal strTxtFilePath As String) As Integer Dim strTxt As String Dim strLines() As String 'Open the text file On Error GoTo CountLineInTxtFile_Error Open strTxtFilePath For Input As #1 'load the text file in strTxt variable strTxt = Input(LOF(1), 1) 'close the text file Close #1 'store the lines strLines = Split(strTxt, vbCrLf) CountLineInTxtFile = UBound(strLines) - LBound(strLines) On Error GoTo 0 Exit Function CountLineInTxtFile_Error: MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure " &_ "CountLineInTxtFile of Module Module1" End Function |