Seek and Ye Shall Find

This code displays the last line of a text file in a message box. Basically, it shows everything after the last linefeed.

Sub FindDStamp()
Dim Fname As String, LastLine As String
Dim Fnum As Long, i As Long
Dim CarRet As String
Fname = “C:DickTesterFindLast2.txt”
Fnum = FreeFile
Open Fname For Input As Fnum
For i = LOF(Fnum) To 1 Step -1
    Seek #Fnum, i
    CarRet = Input(1, #Fnum)
    If CarRet = vbCr Or CarRet = vbCrLf Then
        Line Input #Fnum, LastLine
        MsgBox LastLine
        Close Fnum
        Exit Sub
    End If
Next i
End Sub

I’ve never really had occasion to use this, except to answer a newsgroup post a while back (His last line was a date stamp, that’s why this procedure has a funny name). But it does demonstrate some of the text file handling functions that are available.

The poster who needed this had a very large text file, so starting from the top to get to the last line wasn’t an option for him. Seek to the rescue.

Posted in Uncategorized

One thought on “Seek and Ye Shall Find

  1. hello,

    would you be able to modify this so it will just return the number of lines in the text file?

    I can’t seem to find anything on this…

    thanks!
    Jesse


Posting code? Use <pre> tags for VBA and <code> tags for inline.

Leave a Reply

Your email address will not be published.