Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

seeing first 20 lines in a large text file

Status
Not open for further replies.

DonyBoy

MIS
Apr 22, 2005
26
GB
I have an incredibly large text files that take ages to open. All I'm interested in seeing is the first 20 lines of each of these text files. Is there are clever way of seeing the first 20 lines, without having to open the entire file- in VBA?
 
The following code is something I put together to split large text files into smaller files with a predetermined number of rows. You could amend this fairly easily to just read the first 20 lines (perhaps into a new file or variable?) and then stop:

Code:
Public Sub Splitter(ByVal strSourceFile As String, ByVal strDestinationPath As String, ByVal lngMaxRecordCount As Long)
    Dim SourceFile As Integer
    Dim DestinationFile As Integer
    Dim strRecord As String
    Dim lngRecordCount As Long
    Dim intFileNum As Integer: intFileNum = 1
    
    SourceFile = FreeFile()
    Open strSourceFile For Input As #SourceFile
    DestinationFile = FreeFile()
    Open strDestinationPath & intFileNum & Right$(strSourceFile, 4) For Output As #DestinationFile
    
    Do While Not EOF(SourceFile)
        Line Input #SourceFile, strRecord
        Print #DestinationFile, strRecord
        lngRecordCount = lngRecordCount + 1
        
        If lngRecordCount = lngMaxRecordCount Then
            intFileNum = intFileNum + 1
            lngRecordCount = 0
            Close #DestinationFile
            DestinationFile = FreeFile()
            Open strDestinationPath & intFileNum & Right$(strSourceFile, 4) For Output As #DestinationFile
        End If
    Loop

    Close #SourceFile
    Close #DestinationFile
End Sub

Ed Metcalfe.

Please do not feed the trolls.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top