Home > How-To Library > Text File Handling
How to count number of lines in a text file
**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * **************************************************************** 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
If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.