Re: Count Text File Lines
Quote from Kenneth Hobson;375912
In these routines, I show how to get the count of lines and contents. Change the value for myPath in the Sub test(). If the file does not exist, the count will be shown as 0 and the contents will be shown as "NA".CodeDisplay More
Sub test() Dim myFile As String myFile = "e:\Excel\FileReadWrite\SQLString.txt" MsgBox myFile & ":" & vbCrLf & TXTFileLines(myFile), vbInformation, "Total Number of Line in Text File" MsgBox myFile & ":" & vbCrLf & TXTStr(myFile), vbInformation, "Contents of Text File" End Sub Function TXTFileLines(textFilePath As String) As Long If TXTStr(textFilePath) = "NA" Then TXTFileLines = 0 Exit Function End If TXTFileLines = UBound(Split(TXTStr(textFilePath), vbCrLf)) + 1 '1 added as array elements start at 0 End Function Function TXTStr(filePath As String) As String Dim str As String, hFile As Integer If Dir(filePath) = "" Then TXTStr = "NA" Exit Function End If hFile = FreeFile Open filePath For Binary Access Read As #hFile str = Input(LOF(hFile), hFile) Close hFile TXTStr = str End Function
This is a great solution! Thank you for posting it. Is there any way, in the TXTStr function to not count the row if it is blank? I am counting the lines, before I import them to a temp table, and I want to be able to compare the count from before import to after import.