Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following is my take on this. This is more efficient than the previous two answers if you have a very large file, since we don't have to store the entire file in memory.</p> <pre><code>Option Explicit Private Sub Command_Click() Dim asLines() As String asLines() = LoadLastLinesInFile("C:\Program Files (x86)\VMware\VMware Workstation\open_source_licenses.txt", 400) End Sub Private Function LoadLastLinesInFile(ByRef the_sFileName As String, ByVal the_nLineCount As Long) As String() Dim nFileNo As Integer Dim asLines() As String Dim asLinesCopy() As String Dim bBufferWrapped As Boolean Dim nLineNo As Long Dim nLastLineNo As Long Dim nNewLineNo As Long Dim nErrNumber As Long Dim sErrSource As String Dim sErrDescription As String On Error GoTo ErrorHandler nFileNo = FreeFile Open the_sFileName For Input As #nFileNo On Error GoTo ErrorHandler_FileOpened ' Size our buffer to the number of specified lines. ReDim asLines(0 To the_nLineCount - 1) nLineNo = 0 ' Read all lines until the end of the file. Do Until EOF(nFileNo) Line Input #nFileNo, asLines(nLineNo) nLineNo = nLineNo + 1 ' Check to see whether we have got to the end of the string array. If nLineNo = the_nLineCount Then ' In which case, flag that we did so, and wrap back to the beginning. bBufferWrapped = True nLineNo = 0 End If Loop Close nFileNo On Error GoTo ErrorHandler ' Were there more lines than we had array space? If bBufferWrapped Then ' Create a new string array, and copy the bottom section of the previous array into it, followed ' by the top of the previous array. ReDim asLinesCopy(0 To the_nLineCount - 1) nLastLineNo = nLineNo nNewLineNo = 0 For nLineNo = nLastLineNo + 1 To the_nLineCount - 1 asLinesCopy(nNewLineNo) = asLines(nLineNo) nNewLineNo = nNewLineNo + 1 Next nLineNo For nLineNo = 0 To nLastLineNo asLinesCopy(nNewLineNo) = asLines(nLineNo) nNewLineNo = nNewLineNo + 1 Next nLineNo ' Return the new array. LoadLastLinesInFile = asLinesCopy() Else ' Simply resize down the array, and return it. ReDim Preserve asLines(0 To nLineNo) LoadLastLinesInFile = asLines() End If Exit Function ErrorHandler_FileOpened: ' If an error occurred whilst reading the file, we must ensure that the file is closed ' before reraising the error. We have to backup and restore the error object. nErrNumber = Err.Number sErrSource = Err.Source sErrDescription = Err.Description Close #nFileNo Err.Raise nErrNumber, sErrSource, sErrDescription ErrorHandler: Err.Raise Err.Number, Err.Source, Err.Description End Function </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload