Consulting

Results 1 to 3 of 3

Thread: Solved: Dealing with very large text files

  1. #1
    VBAX Regular
    Joined
    Nov 2007
    Location
    Unfortunately Maryland
    Posts
    15
    Location

    Solved: Dealing with very large text files

    I have an application that creates a CSV data file. Most of the people I deal with want to use excel to view the files and create a chart or two. My issue lies in that the text files often excede 65554 lines which is excels maximum and excel never loads the entire file.

    Is there a way to write some vba code that will split the file up into a series of files with the number of lines that the user specifies. The original file is named something like data001.csv and I want the new files to be data001_001.csv, data001_002.csv etc etc.

  2. #2
    VBAX Mentor anandbohra's Avatar
    Joined
    May 2007
    Location
    Mumbai
    Posts
    313
    Location

    Import large Text files

    Hi This code i found earlier while visiting excel sites(not my code just customized it as per requirement).
    hope this code suits your requirement.

    in the below code amend the values in red shaded line to your requirement

    [VBA]Sub LargeFileImport()
    On Error GoTo anand
    Dim ResultStr As String
    Dim FileName As String
    Dim FileNum As Integer
    Dim counter As Double
    'Ask User for File's Name
    FileName = Application.GetOpenFilename
    'Check for no entry
    If FileName = "" Then End
    'Get Next Available File Handle Number
    FileNum = FreeFile()
    'Open Text File For Input
    Open FileName For Input As #FileNum
    'Turn Screen Updating Off
    Application.ScreenUpdating = False
    'Create A New WorkBook With One Worksheet In It
    Workbooks.Add Template:=xlWorksheet
    'Set The Counter to 1
    counter = 1
    'Loop Until the End Of File Is Reached
    Do While Seek(FileNum) <= LOF(FileNum)
    'Display Importing Row Number On Status Bar
    Application.StatusBar = "Importing Row " & counter & " of text file " _
    & FileName
    'Store One Line Of Text From File To Variable
    Line Input #FileNum, ResultStr
    'Store Variable Data Into Active Cell
    If Left(ResultStr, 1) = "=" Then
    ActiveCell.Value = "'" & ResultStr
    Else
    ActiveCell.Value = ResultStr
    End If
    If ActiveCell.Row = 65536 Then
    'If On The Last Row Then Add A New Sheet
    ActiveWorkbook.Sheets.Add
    Else
    'If Not The Last Row Then Go One Cell Down
    ActiveCell.Offset(1, 0).Select
    End If
    'Increment the Counter By 1
    counter = counter + 1
    'Start Again At Top Of 'Do While' Statement
    Loop
    'Close The Open Text File
    Close
    'Remove Message From Status Bar
    Application.StatusBar = False
    Exit Sub
    anand:
    MsgBox "Oopss.....", vbInformation
    End Sub
    [/VBA]
    Always Mark your Thread as Solved the moment u got acceptable reply (located under Thread tools)
    Practice this & save time of others in thinking for unsolved thread

  3. #3
    VBAX Regular
    Joined
    Nov 2007
    Location
    Unfortunately Maryland
    Posts
    15
    Location
    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •