+ Reply to Thread
Results 1 to 11 of 11

Importing multiple csv files.

  1. #1
    Registered User
    Join Date
    11-09-2006
    MS-Off Ver
    Excel 2010 64 bit
    Posts
    200

    Importing multiple csv files.

    Hello friends!
    I have a list of 250 CSV files. I need to import each of these into their own worksheet (e.g. create worksheet ‘Apple” for file Apple.csv. The data should be placed at cell A1, separated by comma and import parameters saved – do you think VBA can automate this? I have attached a few sample CSV files…Any help hugely appreciated!

    list of files uploaded:

    EURAUD.csv
    DAX.csv
    3M.csv
    SOYUSD.csv
    Alcoa.csv


    Dave
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Importing multiple csv files.

    Try
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-09-2006
    MS-Off Ver
    Excel 2010 64 bit
    Posts
    200

    Re: Importing multiple csv files.

    thank you for this beautiful code....

    I have noticed two issues - can you please help:

    1) The import parameters are not saved with this code - I will need to refresh this data later on so I need to save the query
    2) I am getting this error for the files with the periods:

    Microsoft Visual Basic —
    Run-time error '-2147217865 (80040e37)':
    The Microsoft Access database engine could not find the object
    ‘.E.bay_1440.csv’. Make sure the object exists and that you spell its
    name and the path name correctly. If '.E.bay_1440.csv’ is not a local
    object, check your network connection or contact the server h
    administrator.

    the actual name of the file is <E.bay_1440.csv> - not sure why it is adding the period to the start of the file...

    thanks a lot for your wonderful help Jindon!

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Importing multiple csv files.

    Then try this.
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-09-2006
    MS-Off Ver
    Excel 2010 64 bit
    Posts
    200

    Re: Importing multiple csv files.

    Thank you Jindon - you must be in Tokyo - so some lag between responses...

    thank you for this beautiful macro - always amazed at how such a short macro can do so much!!

    The macro import all fine - it just does not keep the import parameters - how can I keep connections to data so that I can refresh the data afterwards by just pressing REFRESH ALL CONNECTIONS?

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Importing multiple csv files.

    It should update all the data by REFRESHALL.

  7. #7
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Importing multiple csv files.

    Another alternative,

    Please Login or Register  to view this content.
    All this does is open each .CSV file in the indicated directory, then move it to the end of the workbook containing the macro. If a worksheet with a given name already exists, a new one would be added with (2), (3), etc appended to the worksheet name, so none would be skipped.

  8. #8
    Registered User
    Join Date
    11-09-2006
    MS-Off Ver
    Excel 2010 64 bit
    Posts
    200

    Re: Importing multiple csv files.

    Quote Originally Posted by jindon View Post
    It should update all the data by REFRESHALL.
    Hello Jindon - I tried to run it again and it is not saving import parameters (*which normally show when you right click on imported columns) - there are no connections in data view and refresh all does not refresh the imported data - can you please help?

  9. #9
    Registered User
    Join Date
    11-09-2006
    MS-Off Ver
    Excel 2010 64 bit
    Posts
    200

    Re: Importing multiple csv files.

    Quote Originally Posted by hrlngrv View Post
    Another alternative,

    Please Login or Register  to view this content.
    All this does is open each .CSV file in the indicated directory, then move it to the end of the workbook containing the macro. If a worksheet with a given name already exists, a new one would be added with (2), (3), etc appended to the worksheet name, so none would be skipped.
    thanks - does it save import parameters which can then be used to update the files?

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Importing multiple csv files.

    Once you run the code, select any sheet that shows the imported data and [Data] - [Query & Connection] - [Property].
    You will see the connection property.

    It should update anytime you click Refresh all.

  11. #11
    Registered User
    Join Date
    11-09-2006
    MS-Off Ver
    Excel 2010 64 bit
    Posts
    200

    Re: Importing multiple csv files.

    Quote Originally Posted by jindon View Post
    It should update all the data by REFRESHALL.

    Jindon, I just recorded the macro which saves the import parameters:

    With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;C:\Users\Selected\PycharmProjects\pythonProject\data\#3I_1440.csv", _
    Destination:=Range("$A$1"))
    .Name = "#3I_1440"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 437
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
    End With
    End Sub


    do you think you macro can be updated slightly to save the query parameters?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Importing multiple XML files in one go
    By Netlon in forum Excel General
    Replies: 5
    Last Post: 02-13-2018, 03:54 PM
  2. importing multiple. dat files
    By georgeldimenjr in forum Excel General
    Replies: 0
    Last Post: 11-11-2015, 08:00 PM
  3. Importing multiple txt files
    By cesar.crivelli in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-25-2013, 09:40 PM
  4. Replies: 0
    Last Post: 11-27-2012, 01:43 PM
  5. Importing multiple .txt files
    By awdms6 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-09-2010, 05:55 PM
  6. Importing multiple files
    By schnett in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-19-2006, 12:09 PM
  7. importing multiple .dat files!
    By via135 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-29-2006, 03:09 PM

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