Jacxel
Jacxel

Reputation: 1654

Manipulate/copy .CSV data, without opening the file?

I'm trying to optimize some code that takes some test data stored in CSV files does some analysis and copies their data into an excel sheet. This code is often run on hundreds of tests at a time, and its taking about 4.5 seconds per test so it can take hours to complete at times.

I looked up some optimization techniques and cut it down by about .25 seconds per test but I think the bulk of the time is being taken up by excel having to "open" the individual files before it can do anything with them. Is there a way to do this more efficiently?

I am open to answers that involve using another language to compile the files into one big file if that would make things quicker.

Upvotes: 1

Views: 6255

Answers (2)

Jesus Ibarra
Jesus Ibarra

Reputation: 561

I have this function working greate handling lot of CSV files. You need to indicate in cell "D11" the name of folder containing all the CSV files and will combine them into one single file. I handle over 200 files and make it quick. Hope it helps.

Sub CombineAllFilesInADirectory()
    Dim Path            As String 'string variable to hold the path to look through
    Dim FileName        As String 'temporary filename string variable
    Dim tWB             As Workbook 'temporary workbook (each in directory)
    Dim tWS             As Worksheet 'temporary worksheet variable
    Dim aWS             As Worksheet 'active sheet in master workbook
    Dim RowCount        As Long 'Rows used on master sheet
    Dim uRange          As Range 'usedrange for each temporary sheet
    Dim mWB_comb        As Workbook 'master workbook exclusivo de esta funcion

    Path = Sheets("CombineFiles").Range("D11").Value
    Application.EnableEvents = False 'turn off events
    Application.ScreenUpdating = False 'turn off screen updating
    Set mWB_comb = Workbooks.Add(1) 'create a new one-worksheet workbook
    Set aWS = mWB_comb.ActiveSheet 'set active sheet variable to only sheet in mWB
    If Right(Path, 1) <> Application.PathSeparator Then 'if path doesnt end in "\"
        Path = Path & Application.PathSeparator 'add "\"
    End If
    FileName = Dir(Path & "*.csv", vbNormal) 'set first file's name to filename variable
    Application.StatusBar = "reading files, please wait."
    Do Until FileName = "" 'loop until all files have been parsed
        If Path <> ThisWorkbook.Path Or FileName <> ThisWorkbook.Name Then
            Set tWB = Workbooks.Open(FileName:=Path & FileName) 'open file, set to tWB variable
            For Each tWS In tWB.Worksheets 'loop through each sheet
                Set uRange = tWS.Range("A4", tWS.Cells(tWS.UsedRange.Row + tWS.UsedRange.Rows.count - 1, _
                tWS.UsedRange.Column + tWS.UsedRange.Columns.count - 1)) 'set used range
                If RowCount + uRange.Rows.count > 65536 Then 'if the used range wont fit on the sheet
                    aWS.Columns.AutoFit 'autofit mostly-used worksheet's columns
                    Set aWS = mWB_comb.Sheets.Add(After:=aWS) 'add a new sheet that will accommodate data
                    RowCount = 0 'reset RowCount variable
                End If
                If RowCount = 0 Then 'if working with a new sheet
                    aWS.Range("A1", aWS.Cells(3, uRange.Columns.count)).Value = tWS.Range("A1", _
                    tWS.Cells(3, uRange.Columns.count)).Value 'copy headers from tWS
                    RowCount = 3 'add one to rowcount
                End If
                aWS.Range("A" & RowCount + 1).Resize(uRange.Rows.count, _
                uRange.Columns.count).Value = uRange.Value 'move data from temp sheet to data sheet
                RowCount = RowCount + uRange.Rows.count 'increase rowcount accordingly
            Next 'tWS
            tWB.Close False 'close temporary workbook without saving
        End If
        FileName = Dir() 'set next file's name to FileName variable
    Loop
    Application.StatusBar = "Ready"
    mWB_comb.Sheets(1).Select 'select first data sheet on master workbook
   Application.EnableEvents = True 're-enable events
    Application.ScreenUpdating = True 'turn screen updating back on
     'Clear memory of the object variables
    Set tWB = Nothing
    Set tWS = Nothing
    Set mWB_comb = Nothing
    Set aWS = Nothing
    Set uRange = Nothing
End Sub

Upvotes: 1

chris neilsen
chris neilsen

Reputation: 53137

I would open them as text rather than workbooks:

Sub ReadCSV()
    Dim MyString As String
    Open "C:\path\text.csv" For Input As #1 ' Open file for input. 
    Do While Not EOF(1) ' Loop until end of file.
        Line Input #1, MyString ' Read a line into variable
        Debug.Print MyString ' Print data to the Immediate window.
    Loop
    Close #1 ' Close file.

End Sub

This will be much faster than opening as a workbook

Upvotes: 3

Related Questions