+ Reply to Thread
Results 1 to 7 of 7

Importing multiple large .csv files for analysis (Run Time Error - 7)

  1. #1
    Registered User
    Join Date
    10-21-2011
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2010
    Posts
    5

    Importing multiple large .csv files for analysis (Run Time Error - 7)

    Howdy,

    Firstly, I am a total beginner when it comes to VBA programming but I am keen to learn and dont want to be dissuaded in doing so by a few bumps in the road.

    I have 10 .csv files each containing 800,000+ rows (all in the same folder entitled 1.csv, 2.csv, 3.csv etc). I am looking to bring them into 1 excel workbook (each in a different worksheet) and from there pivot table each worksheet to produce an overall 'average' pivot table.

    The first issue I encountered was being able to copy and paste such large .csv files into Excel (Excel 2010). The only way I have managed to do so manually was by importing the data through Data>Get Extrenal Data> From Text.

    By way of viewing macros and similar codes online I have come up with the following:

    --------------------------------------------------------

    Sub TEST()
    '
    ' TEST Macro
    '

    '
    For i = 1 To 10
    Sheets.Add
    ActiveSheet.Name = i & ".csv"
    With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;D:\Erskine Bridge 2012\## Calibration ##\## Models ##\Base Model - Erskine Bridge 2012\Log\Path Journey Time Runs\" & i & ".csv" _
    , Destination:=Range("$A$1"))
    .Name = "1"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 850
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _
    , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
    End With
    ActiveWorkbook.Connections("1").Delete
    Next i
    End Sub

    ----------------------------------------------------

    At the moment Im just trying to investigate how to bring in the .csv automat

    When I run the code it works for 1.csv, creates the sheet for 2.csv and gives me the error 'Run Time Error 7 - out of memory'. This is pretty much my first go at any VBA programming so Im trying not to give up on this so any help would be greatly appreciated.

    1.csv, 2.csv, 3.csv files are each 150Mb

    Thanks in advance

    Richard

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Importing multiple large .csv files for analysis (Run Time Error - 7)

    Richard

    8 million rows of data, even spread across multiple worksheets, is a bit much for Excel I would think.

    Even if you could import all the data it would be a nightmare to do anything with it.

    Have you considered using Access or some other database, perhaps just to store the data?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    10-21-2011
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Importing multiple large .csv files for analysis (Run Time Error - 7)

    Norie,

    I can write macros to pivot table the original .csv's and then bring them into one workbook as 10 individual pivot tables but I was looking for something to streamline the process from 11 macros (ie - 10 csv + 1 overall to import them) to a single macro. Ideally I would then attempt to set up a prompt to select the .csv folder location and a prompt for the number of runs and it can be transferable to all my projects.

    As mentioned, all I am looking for is an average pivot table of the 10 individual pivot tables - I cant see this taking more than 15 mins to produce the 'average' whereas the long-winded method will take x5 longer (imo). Im interested in the least amount of user-input as well so I can send it to colleagues.

    Richard

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Importing multiple large .csv files for analysis (Run Time Error - 7)

    Richard

    Not sure what you mean.

    If you have code to create a pivot table for one of the CSVs it should be straightforward to adapt that code to work with multiple
    CSVs.

    It's also pretty straighforward to have the user pick a folder etc.

    PS How long does it take to create one pivot table?

  5. #5
    Registered User
    Join Date
    10-21-2011
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Importing multiple large .csv files for analysis (Run Time Error - 7)

    It takes about 5 mins to run the macro to create the pivot table.

    I open 1.csv, the macro adds a calculated column, creates the pivot table and saves the whole thing as .xls

    I assumed that you had to open up say 1.csv and then run the macro, then open up 2.csv then run the macro, open up 3.csv then run the macro...etc etc? then open a new .xls file and bring each of these pivots into individual worksheets and from there I can insert a new sheet which calculates the average.

    If you are able to somehow open up an excel sheet and run a macro that opens up 10 csvs in the background, pivots them and then averages them then great, Im all for that !!?

    Richard

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Importing multiple large .csv files for analysis (Run Time Error - 7)

    What I'm talking about is taking whatever code you have now and adapting it for multiple CSVs.

    That might be by using a simple loop if the names of the CSVs are 1.csv, 2.csv etc

    Or looping through a user selected directory.

    Or loping through a set of files the user has selected.

    Whatever it is you would have one macro that created the pivot tables for each CSV.

    It wouldn't run in the background though.

  7. #7
    Registered User
    Join Date
    10-21-2011
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Importing multiple large .csv files for analysis (Run Time Error - 7)

    This sounds promising.

    So as I understand it:

    1. I have a blank Excel workbook (WB1).
    2. I write code in this workbook that will loop through my 10 csv's
    3. The code opens up csv1, performs the pivot table and then will copy that pivot table into Sheet1 (renaming this csv1), close csv1.
    4. once it has done this the loop will direct it toward csv2 which is then opened, same as above (sheet2 renamed as csv2), close csv2.

    Could you point me towards any similar code (doesnt have to be csv or pivots, just something that shows how to loop through a directory)?

    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1