• Last line number in a text file (2002/SP3)

    Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » Last line number in a text file (2002/SP3)

    Author
    Topic
    #455041

    Is there a way to get the total number of lines in a text file with out reading the whole file line by line, ie, what’s the last line number?

    Viewing 1 reply thread
    Author
    Replies
    • #1131205

      You could open the file in Word, and get ActiveDocument.Paragraphs.Count, or open the file in Excel, and retrieve ActiveSheet.UsedRange.Rows.Count.

      • #1131207

        Thanks Hans. But is there a way to do this without opening the text file directly in Word or Excel. Is there a shell command that looks at the file and gives you the number of lines/rows?

        • #1131209

          Not as far as I know. Text files are just that – they don’t contain any metadata.

    • #1131208

      You could try this piece of code in an excel workbook assuming the file is in a fixed location

      Sub CountTextFile()

      Dim filenum As Integer
      Dim count As Long
      Dim tmp As String
      filenum = FreeFile
      Open “c:test.txt” For Input As filenum
      Do While Not EOF(filenum)
      Line Input #filenum, tmp$
      count = count + 1
      Loop
      Close #filenum

      MsgBox count
      End Sub

    Viewing 1 reply thread
    Reply To: Last line number in a text file (2002/SP3)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: