PDA

View Full Version : Solved: Dealing with very large text files



pcollins
11-07-2007, 04:59 PM
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.

anandbohra
11-07-2007, 10:23 PM
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

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

pcollins
11-08-2007, 02:55 PM
Thank you